Archived

This topic is now archived and is closed to further replies.

Stupid std::map question...

This topic is 5975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I''ve some stupid question about std::map this is my map definition : typedef map TextureMap; TextureMap m_TextureMap; so i want delete all cTexture like these : VOID DeleteTextureMap (VOID) { for (TextureMap::iterator i=m_TextureMap.begin (); i!=m_TextureMap.end (); i++) { delete (*i); // BUT this won''t work !!! } m_TextureMap.clear (); } How does it corectly work??? Thx Imp bugs are uncommented features...

Share this post


Link to post
Share on other sites
an object in a map is composed of a key and the value.

iterator objects from a map will return std::pair.

so call

delete( i->second );

first is the key, second is your object

Share this post


Link to post
Share on other sites
Somewhat off-topic, but..

There are two inefficiencies in your deletion algorithm:
- using i++ instead of ++i creates a temporary iterator that is never used
- you''re evaluating m_textureMap.end () every time through the loop.

You could try to fix each of these manually, or you could write a functor that uses for_each to delete each entry. I would suggest the latter.

Share this post


Link to post
Share on other sites