Zubizaretta
Zubizaretta

Reputation: 157

c++ array evaluation

Is there a way to "bulk evaluate" the contents of an array in C++? For example, let int numbers[10]={23,42,12,42,53,10,0,0,0,0}. Is there a way to loop through each element in the array and check whether the element meets a specified condition?

In short, I'd like to be able to do something like:

if(every element in the array meets arbitrary condition)
do this

or

if(array element from m to array element n==something)
do this

For small arrays, I know I could use something like: if(numbers[0]==blabla) && if(numbers[n]==blabla), then do this, but obviously this is not a realistic solution when it comes to evaluating very large arrays.

Upvotes: 2

Views: 2789

Answers (6)

Joe McGrath
Joe McGrath

Reputation: 1501

" if(every element in the array meets arbitrary condition) do this " with STL:

bool IsOdd (int i) 
{
  return ((i%2)==1);
}
  //...
{
  vector<int> myvector;
  vector<int>::iterator it;

  myvector.push_back(2);
  myvector.push_back(4);
  myvector.push_back(6);
  myvector.push_back(8);

  it = find_if (myvector.begin(), myvector.end(), IsOdd);
  if (it == myvector.end())
    cout<< "No Odd numbers";
  }

" if(every element in the array meets arbitrary condition) do this " without STL

numbers[10]={2,4,6,8,10,12,14,16,18,20}
bool oddExist=false;
for (int i =0;i<10;++i)
  {
     if ( numbers[i]%2 )
     {                      //added
       oddExist=true;     
       break;               //added  for efficiency, was not in 
     }                      //        first post. 
  }
      if (!oddExist)
        cout<< "No Odd numbers";

"if(array element from m to array element n==something) do this" with STL

void printNumber (int i) 
{
  cout  << i;
}

  // ... 

  vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(20);
  myvector.push_back(30);
  myvector.push_back(40);

  for_each (myvector.begin(), myvector.end(), printNumber);

"if(array element from m to array element n==something) do this" without STL

numbers[10]={2,4,6,8,10,12,14,16,18,20}

for (int i =0;i<10;++i)
   cout << numbers[i];

Upvotes: 3

Loki Astari
Loki Astari

Reputation: 264471

if(every element in the array meets arbitrary condition)
do this

if (std::find_if(&a[0], &a[size], testUnaryFunction) == &a[size])
{
    do this  // if all elements in i => [0-size) testUnaryFunction(a[i]) return false
}

if(array element from m to array element n==something)
do this

if (std::equal(&a[0], &a[size], &b[0], testBinaryFunctin))
{
    do this  // if all elements in i => [0-size) testBinaryFunctin(a[i], b[i]) returns true
}

If you have C++11 then you can use closures:

if (std::find_if(&a[0], &a[size], [](type const& val) { return val%2 == 0;}) == &a[size])
{
    do this // if all elements are odd
}

if (std::equal(&a[0], &a[size], &b[0], [](type const& lhs, type const& rhs) { return lhs == rhs;}))
{
    do this  // if arrays a and b are equal.
}

Upvotes: 0

paul23
paul23

Reputation: 9435

Just for competeness sake:

you can also use the ranged based for, however you compiler has to have implemented that part of the C++11 version already. I know microsoft visual C++ (2010) hasn't. Though I believe GCC 4+ has already.

int my_array[] = {2,4,6,8,10,12,14,16,18,20};
for (int &x : my_array) {
    std::cout << x << std::endl;
}

Alternativelly a good thing is to use std::for_each together with a lambda function, I found msdn: to be very good in explaining lambda's.

int my_array[] = {2,4,6,8,10,12,14,16,18,20};
std::for_each(my_array[0], my_array[10], [&] (int* iter) {
    std::cout << *iter << std::endl;
});

Or simply the for statement as described above.

Upvotes: 0

juanchopanza
juanchopanza

Reputation: 227418

Since your condition seems to be that every element satisfy a condition, it is probably more efficient to use an algorithm such as std::find or std::find_if. For find_if, you can define a functor that returns true when your condition is not satistied, and the algorithm will stop at the first occurrence, instead of looping through the whole array.

Upvotes: 2

MoonBun
MoonBun

Reputation: 4402

you probably mean "for"

for(int i = 0; i < size_of_array; i++)
  if( the_condition_function(numbers[i])){
     //do this
  }

Upvotes: 4

Alok Save
Alok Save

Reputation: 206546

Is there a way to loop through each element in the array and check whether the element meets a specified condition?

You can use std::for_each algorithm.

Upvotes: 2

Related Questions