How would I use for_each to delete every value in an STL map?
Posted
by stusmith
on Stack Overflow
See other posts from Stack Overflow
or by stusmith
Published on 2010-03-17T15:26:19Z
Indexed on
2010/03/17
15:31 UTC
Read the original article
Hit count: 171
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).
© Stack Overflow or respective owner