c++booststlstl-algorithmboost-ptr-container

STL algorithm to delete all the objects in a container?


Is there a STL utility/algorithm to do delete *the_object_iterator; on all the objects? So that I can clear() safely? The STL container is a set and the objects are pointers to C++ classes created with new.

Boost seems to be the best solution. My goal was to avoid copy-construction on noncopyable classes.


Solution

  • As far as I know, there is no standard algorithm to delete all objects. However, you can build up one easily:

    template< typename T > invoke_delete( T* ptr ){ delete ptr; }
    
    std::for_each( set.begin(), set.end(), &invoke_delete< set_value_type > );