I am writing an imagehandler for an engine. So far it’s going pretty good (I think) but I need help with deleting images. I have experience with vectors but not with maps.
The image handler has an std::map which has 2 elements:
std::map<std::string, SDL_Surface*> image_list_;
std::map<std::string, SDL_Surface*>::iterator it;
Then i have 2 methods in my ImageHandler class:
void AddImage(std::string/*file_name*/);
void DeleteImage(std::string/*file_name*/);
Here are the guts of these 2 methods:
bool ImageHandler::AddImage(std::string file_name)
{
SDL_Surface* temp = NULL;
if ((temp = Image::Load(file_name)) == NULL)
return false;
image_list_.insert(std::pair<std::string, SDL_Surface*>(file_name, temp));
SDL_FreeSurface(temp);
return true;
}
bool ImageHandler::DeleteImage(std::string file_name)
{
if (image_list_.empty()) return;
it = image_list_.find(file_name);
if (!it) return false;
image_list_.erase(it);
return true;
}
I haven’t compiled this code so I am not aware of any syntax errors. If any exist you can just look past those.
I think my DeleteImage method will remove it from the map but to avoid memory leaks when it loads an image I need to do this:
SDL_FreeSurface(SDL_Surface*);
So I think I need to access an iterator’s second element at a specific map index. Am I doing it right so far and how would I be able to do this?
Like this: