How would I use for_each to delete every value in an STL map?

后端 未结 4 2108
执念已碎
执念已碎 2021-02-09 09:50

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

4条回答
  •  余生分开走
    2021-02-09 10:23

    OK, I found out how to do it in one line... but I don't think I would ever actually do the following in real code!

    std::for_each( mayMap.begin()
                 , myMap.end()
                 , boost::bind( &boost::checked_delete
                              , boost::bind( &stdext::hash_map::value_type::second, _1 ) ) );
    

    However I'm going to accept GMan's answer because I like his idea of a wrapped container, and my answer, despite being one line as requested, is just plain nasty.

提交回复
热议问题