下面是用于说明我的问题的相同的C++代码
我知道其他工作方法,但有兴趣知道下面的代码是否错误?用指针删除列表也是必须清除的对象
Void pupulatelist()
{
//populating the list with some int pointers,in actual i have some other objects to delete when accessed each time.
for(int i =0;i<5;i++)
{
int *p = new int(i);
list.push_back(p);
}
//want to delete and erase the contents of the above list
// if i dont use erase my actual code is crashing.
for(std::list<int *>::iterator iter = list.begin(); iter != list.end(); ++iter)
{
delete(*iter);
list.erase(iter--);
}
}
取消'list.erase'并在循环之后调用'list.clear'。 (另外,请不要使用类型名称作为变量名称。) – molbdnilo