C++ for each with Functor
#include <iostream> #include <algorithm> #include <vector> using namespace std; class MyFunctor// w w w. j a v a 2 s . c om { public: void operator()(int x) { cout << x << endl; } }; void ProcessVector(vector<int>& vect) { MyFunctor Func; for_each(vect.begin(), vect.end(), Func); } int main() { vector<int> myV; myV.push_back(1); myV.push_back(2); myV.push_back(3); myV.push_back(4); ProcessVector(myV); }