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
If at all possible, you should use smart pointers in your map.
The use of smart pointers here removes the need to refactor and debug member deletion. One less memory management to worry about going forward. Any time I use new/delete I think really hard about whether that's needed. A personal "code smell" (per Martin Fowler), if you like.
Of course, if your old code returns a map, then the for_each
approach is probably your best bet - but if you had some hand in creating the map, I'd recommend using smart pointers.