, . , , for_each .
std::for_each . for_each_if . , (4, 6 8).
#include <iostream>
#include <vector>
using namespace std;
struct is_even {
typedef bool return_type;
bool operator() (const int& value) {return (value%2)==0; }
};
struct doprint {
bool operator() (const int& value) { std::cout << value << std::endl; }
};
template <class InputIterator, class Predicate, class Function>
void for_each_if(InputIterator first, InputIterator last, Function f, Predicate pred)
{
while ( first != last )
{
if (pred (*first))
f(*first++);
else
first ++;
}
}
int main()
{
std::vector<int> v;
v.push_back( 4 );
v.push_back( 5 );
v.push_back( 6 );
v.push_back( 8 );
for_each_if( v.begin(), v.end(), doprint(), is_even());
return 0;
}