我是一个iOS开发人员,我不熟悉C++.So,我不能理解为什么imageMap的内容不能通过clear()方法删除?
struct ImageNode
{
string key;
UIImage *image;(UIImage is a class in Objective-C)
};
struct ImagePointer
{
list<ImageNode *>::iterator it;
bool isLRUQueue;
};
list<ImageNode *> FIFOQueue;
list<ImageNode *> LRUQueue;
map<string, ImagePointer *> imageMap;
//clear method
- (void)clear
{
//why need for loop???
for (auto it = imageMap.begin(); it != imageMap.end(); it++)
{
delete *it->second->it;
delete it->second;
}
FIFOQueue.clear();
LRUQueue.clear();
imageMap.clear();
}发布于 2018-03-26 00:17:11
我不明白为什么clear()不能删除imageMap的内容
请考虑,即使您确实将指针存储在列表中,也不意味着当您从列表中删除这些指针时,您想要删除它们:
std::list<int*> l;
int i;
l.push_back(&i);
l.clear; // if list deleted the pointer contained within the list
// then bad things would happenstd::list不能读懂你的想法,也不能知道你是否想要删除指针。当需要删除指针时,必须将其删除。Clear只会从列表中删除元素。
https://stackoverflow.com/questions/49477696
复制相似问题