Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
Created October 14, 2011 13:27
Show Gist options
  • Save stevedoyle/1287095 to your computer and use it in GitHub Desktop.
Save stevedoyle/1287095 to your computer and use it in GitHub Desktop.
Functor for deleting objects in a container using for_each
template <class T>
class DeleteObj
{
public:
void operator()(T*obj) { delete obj; }
};
// Sample usage ...
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(), DeleteObj<Foo>());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment