C++ examples for STL Algorithm:count_if
Count element in a sequence for a condition with algorithm count_if
#include <iostream> #include <algorithm> // algorithm definitions #include <numeric> // accumulate is defined here #include <vector> #include <iterator> using namespace std; bool greater9(int); // predicate function prototype int main()/*from w w w . j av a2s . co m*/ { const int SIZE = 10; int a1[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; vector< int > v(a1, a1 + SIZE); // copy of a1 ostream_iterator< int > output(cout, " "); // count number of elements in v2 that are greater than 9 int result = count_if(v.begin(), v.end(), greater9); cout << "\nNumber of elements greater than 9: " << result; } // determine whether argument is greater than 9 bool greater9(int value) { return value > 9; } // end function greater9