#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
template <typename elementType>
bool IsEven (const elementType& number)
{
return ((number % 2) == 0);
}
int main ()
{
vector <int> v;
for (int nNum = -9; nNum < 10; ++ nNum)
v.push_back (nNum);
vector <int>::const_iterator i;
for ( i = v.begin (); i != v.end (); ++ i )
cout << *i << ' ';
cout << v.size () << "' elements" << endl;
// Use the count_if algorithm with the unary predicate IsEven:
size_t nNumEvenElements = count_if (v.begin (),v.end (), IsEven <int> );
cout << "Number of even elements: " << nNumEvenElements << endl;
cout << "Number of odd elements: ";
cout << v.size () - nNumEvenElements << endl;
return 0;
}
|