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

后端 未结 4 2098
执念已碎
执念已碎 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:27

    Have you tried using BOOST_FOREACH ? That should allow you to do that in a line without creating your own functor.

    I have not tested the following code but it should look something like this(if not exactly):

    typedef stdext::hash_map MyMapType; //see comment.
    BOOST_FOREACH( MyMapType::value_type& p, myMap )
    {
        delete p.second;
    }
    

    Well thats more than 1 line, due to the typedef :)

提交回复
热议问题