Thread: Is this correct? (Remove and delete all elements from a STL MAP)

  1. #1
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31

    Is this correct? (Remove and delete all elements from a STL MAP)

    What I want to do is loop through all elements inside the MAP container and then delete it.

    Lastly, just invoke clear() on the map to remove all elements.

    Is my code correct?

    Code:
    MyMap::iterator it;
    
    for (it = MyMap.begin(); it != MyMap.end(); ++it)
    {
        delete (*it).second;
    }
    
    MyMap.clear();
    -dc

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Not sure about the *. instead of ->, might work, but this is what I do:
    Code:
    std::map<int>::iterator i;
    
    i = Map.begin();
    while(i != Map.end())
    {
      delete i->second;
      i++;
    }
    
    Map.clear();
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Is my code correct?
    It's impossible to know without seeing your declaration of your map<>. When you declare an iterator for your map, you have to specify the type of the map<>, and the type of your map depends on what's stored in it, e.g.:
    Code:
    map<string, aClass*> myMap;
    ....
    map<string, aClass*>::iterator i = myMap.begin();
    Not sure about the *. instead of ->
    They are equivalent, but -> is much easier to type.
    Last edited by 7stud; 03-06-2006 at 02:49 PM.

  4. #4
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    Quote Originally Posted by 7stud
    It's impossible to know without seeing your declaration of your map<>. When you declare an iterator for your map, you have to specify the type of the map<>, and the type of your map depends on what's stored in it, e.g.:
    Code:
    map<string, aClass*> myMap;
    ....
    map<string, aClass*>::iterator i = myMap.begin();
    You're right, it is defined like this...
    Code:
    std::map<std::string, Foo*> MyMap;
    -dc

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Of course, all the values stored in the map should either be null pointers or pointers to memory allocated with new, and then the code would be ok.

    Technically, it might be considered undefined behavior to have a deleted pointer still stored in the map, but in the real world I wouldn't worry about it.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    it's also good practice to wrap this kind of usage in a class, so that the destructor will automatically clean up the map.

    [Ignore if you don't like boost]
    Alternatively, use a map of boost.shared_ptr's or a boost.ptr_map which will clean up for you.
    [/Ignore if you don't like boost]
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. Pointer Elements & STL Containers :: STL
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 08:13 PM