For anyone interested, here is a more detailed example of how to use for_each
class Object { public: int run(){} int run2(char c){} }; vector<Object> v(10); char c = 'a';
If you want to send a parameter to your function (maximum for C ++ 03), you can
for_each(v.begin(), v.end(), std::bind2nd( std::mem_fun_ref(&Object::run2), c));
Note that you are linking the second argument. The first is the this pointer for the current object. Do you remember that any member function always takes this as the first parameter?
And the lambda path (C ++ 11) is much nicer!
for_each( v.begin(), v.end(), [&] (const Object& val) { val.run();
Finally, the boost :: lambda way is for the situation where you have an argument. As for C ++ 11, it easily extends to more parameters
for_each(v.begin(), v.end(), bind(&Holder::run, _1, c) );
source share