Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 689201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:18:28+00:00 2026-05-14T02:18:28+00:00

Suppose I have a STL map where the values are pointers, and I want

  • 0

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).

Note: I have subsequently found and listed a one-line answer below… but the code is pretty awful so I’ve accepted GMan’s saner answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:18:29+00:00Added an answer on May 14, 2026 at 2:18 am

    You have to make a function object:

    struct second_deleter
    {
        template <typename T>
        void operator()(const T& pX) const
        {
            delete pX.second;
        }
    };
    
    std::for_each(myMap.begin(), myMap.end(), second_deleter());
    

    If you’re using boost, you could also use the lambda library:

    namespace bl = boost::lambda;
    std::for_each(myMap.begin(), myMap.end(), second_deleter(),
                    bl::bind(bl::delete_ptr(), 
                    bl::bind(std::select2nd<myMap::value_type>(), _1));
    

    But you might try the pointer containers library which does this automatically.

    Note you are not using a map, but a hash_map. I recommend you switch to boost’s unordered_map, which is more current. However, there doesn’t seem to be a ptr_unordered_map.

    For safety, you should wrap this thing up. For example:

    template <typename T, typename Deleter>
    struct wrapped_container
    {
        typedef T container_type;
        typedef Deleter deleter_type;
    
        wrapped_container(const T& pContainer) :
        container(pContainer)
        {}
    
        ~wrapped_container(void)
        {
            std::for_each(container.begin(), container.end(), deleter_type());
        }
    
        T container;
    };
    

    And use it like:

    typedef wrapped_container<
                boost::unordered_map<int, Foo*>, second_deleter> my_container;
    
    my_container.container./* ... */
    

    This ensures no matter what, your container will be iterated through with a deleter. (For exceptions, for example.)

    Compare:

    std::vector<int*> v;
    v.push_back(new int);
    
    throw "leaks!"; // nothing in vector is deleted
    
    wrapped_container<std::vector<int*> > v;
    v.container.push_back(new int);
    
    throw "no leaks!"; // wrapped_container destructs, deletes elements
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.