小编Tib*_*bor的帖子

如何释放指针向量?

如何在指针向量中释放内存?这是代码:

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.

c++ vector object erase delete-operator

5
推荐指数
2
解决办法
2642
查看次数

标签 统计

c++ ×1

delete-operator ×1

erase ×1

object ×1

vector ×1