我正在爲引擎寫一個圖像處理程序。到目前爲止,它非常好(我認爲),但我需要幫助刪除圖像。我有vector
s的經驗,但沒有map
s的經驗。std :: map幫助圖像處理程序類
所述的圖像處理程序具有的std ::地圖,其具有2個元件:
std::map<std::string, SDL_Surface*> image_list_;
std::map<std::string, SDL_Surface*>::iterator it;
然後我有2種方法在我ImageHandler類:
void AddImage(std::string/*file_name*/);
void DeleteImage(std::string/*file_name*/);
以下是這2種方法的膽:
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;
}
我還沒有編譯此代碼,所以我不知道任何語法錯誤。如果有的話,你可以看看那些。
我想我DeleteImage
方法將從map
刪除,但要避免內存泄漏,當加載圖像,我需要做到這一點:
SDL_FreeSurface(SDL_Surface*);
所以我覺得我需要訪問迭代器的秒元素在特定的地圖索引處。我到目前爲止做得對嗎?我該如何做到這一點?
SDL_FreeSurface(IT->第二); image_list_.erase(它); – Jagannath