How would I use for_each to delete every value in an STL map?
- by stusmith
Suppose I have a STL map where the values are pointers, and I want to delete them all. How would I represent the following code, but making use of std::for_each? I'm happy for solutions to use Boost.
for( stdext::hash_map<int, Foo *>::iterator ir = myMap.begin();
ir != myMap.end();
++ir )
{
delete ir->second; // delete all the (Foo *) values.
}
(I've found Boost's checked_delete, but I'm not sure how to apply that to the pair<int, Foo *> that the iterator represents).
(Also, for the purposes of this question, ignore the fact that storing raw pointers that need deleting in an STL container isn't very sensible).