C++ examples for STL Algorithm:for_each
Defining a Problem Solved by Lambda Expressions
#include <iostream> #include <algorithm> #include <vector> using namespace std; class MyFunctor/* w w w .ja va 2 s . c o m*/ { 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); }