Skip to main content
gamedev.net
🔒 Locked

Stupid std::map question...

Started by Impz0r Aug 4, 2001 at 7:23 AM 3 replies 450+ views
Original Post
Impz0r
Impz0r
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...
Stay Evil & Ugly!
Gorg
Gorg
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
Impz0r
Impz0r
thank you m8 :D

Imp
Stay Evil & Ugly!
Stoffel
Stoffel
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.
Impz0r
Impz0r
hey Stoffel thanks for your optimize tricks i would test these out :D

Imp
Stay Evil & Ugly!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.