5
/* STL algorithms that take a function argument don't care whether it's a real
6
function, a member function, or a function object, so long as they can use the
7
notation f() to call it. The problem is that you can't call a member function
8
directly; you need to call it for a pointer or an object */
12
template <class T> int func(T &val) {
19
DataClass(const char *ch) {
35
MyClass(string pref) {
39
void operator ()(string &val) {
40
cout << prefix << val << endl;
50
/* Calling just a function with elements as an argument */
53
for_each(v.begin(), v.end(), func<string>);
56
/* Calling a method from element objects
57
mem_fun_ref() function binds the class function with a reference to
58
object allowing to call object method.
60
Also exists mem_fun doest the same for pointers
65
for_each(v1.begin(), v1.end(), mem_fun_ref(&DataClass::func));
69
for_each(v.begin(), v.end(), MyClass("prefix: "));