Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Clearing pointers in a std::map

This topic is 4858 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

I have a
std::map<string, SDL_Surface*> texture;
Its a map filled with sdl surfaces, I can access them with string so I just can say the name of the texture I want and get it. Problem is, when the texture holding class is deleted, should all the surface pointers in this map be. But is there any way to loop through it? Since Im accessing the members with strings rather than numbers I cant loop through it as I would be able to if it was a std::vector.

Share this post


Link to post
Share on other sites
Advertisement
Look into using iterators. Specifically, the following will loop through the map:


for(std::map<string, SDL_Surface*>::iterator i = texture.begin(); i != texture.end(); ++i)
{
//Use i
}

Share this post


Link to post
Share on other sites
You can iterate through a map with iterators.


for (std::map<std::string, SDL_Surface *>::iterator iter = texture.begin();
iter != texture.end();
++iter) {
// do stuff with the iterator
}


Alternately you can try hold something like boost::weak_ptr<SDL_Surface> instead of a normal pointer which would be able to detect if surface goes out of use.

Share this post


Link to post
Share on other sites
Use iterators to loop through your map:


std::map<string, SDL_Surface*>::iterator iter = texture.begin();
std::map<string, SDL_Surface*>::iterator end= texture.end();

for(iter; iter != end; iter++)
{
delete iter->second;
}

texture.clear;


EDIT: Man you guys are fast at replying.

Share this post


Link to post
Share on other sites
Hi bud,

You can get an iterator to a map and delete the whole collection.


std::map<string, SDL_Surface*> texture;
std::map<string, SDL_Surface*>::iterator iter;

for ( iter = texture.begin();
iter != texture.end();
++iter )
{
delete iter->second;
}

texture.clear();


Sorry don't have msdn in front of me so that is likely to be close but might not be quite right.

Hope it helps,

Dave

EDIT: Got completely thrashed to it then...

Share this post


Link to post
Share on other sites
Thanks for the many, although ver similiar :P, responses. Rating++ to everyone, even you that didnt type an answer in under two seconds. :P

Cheers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!