Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
Created October 14, 2011 13:55
Show Gist options
  • Save stevedoyle/1287180 to your computer and use it in GitHub Desktop.
Save stevedoyle/1287180 to your computer and use it in GitHub Desktop.
Calling a member function on every item in a container using for_each and mem_fun
for_each(foos.begin(), foos.end(), mem_fun(&Foo::doStuff));
// Example ...
class Foo
{
public:
Foo();
void doStuff();
};
int main()
{
vector<Foo*> foos;
for(int i=0; i<10; ++i)
foos.push_back(new Foo());
for_each(foos.begin(), foos.end(), mem_fun(&Foo::doStuff));
...
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment