如何在指针向量中释放内存?这是代码:
class A
{
private:
int x,y,z;
public:
A(param1, param2, param3)
{
x=param1;
y=param2;
z=param3;
}
~A()
{
//prompts an alertbox, warning me about the successful call of the destructor;
}
};
...
vector<A*> list;
list.push_back(new A(1,2,3));
list.erase(list.begin()+index);//SHOULD delete the object from the memory;
list.clear();
Run Code Online (Sandbox Code Playgroud)
我发现.erase()没有释放内存,也没有调用析构函数; 我尝试delete在每个列表条目上使用迭代,但在一次迭代后崩溃.已经检查列表条目是否已经为NULL,以避免任何错误.我错过了什么吗?另外,我必须只使用STL,不需要Boost.