The following code example is taken from the book
The C++ Standard Library - A Tutorial and Reference, 2nd Edition
by Nicolai M. Josuttis, Addison Wesley Longman, 2012
Copyright © 2012 by Pearson Education, Inc. and Nicolai M. Josuttis
#include <functional>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
using namespace std::placeholders;
class Person {
private:
string name;
public:
Person (const string& n) : name(n) {
}
void print () const {
cout << name << endl;
}
void print2 (const string& prefix) const {
cout << prefix << name << endl;
}
//...
};
int main()
{
vector<Person> coll
= { Person("Tick"), Person("Trick"), Person("Track") };
// call member function print() for each person
for_each (coll.begin(), coll.end(),
bind(&Person::print,_1));
cout << endl;
// call member function print2() with additional argument for each person
for_each (coll.begin(), coll.end(),
bind(&Person::print2,_1,"Person: "));
cout << endl;
// call print2() for temporary Person
bind(&Person::print2,_1,"This is: ")(Person("nico"));
}