例如,我有下一个代码:

#include <set>
using namespace std;

struct SomeStruct
{
    int a;
};

int main ()
{
    set<SomeStruct *> *some_cont = new set<SomeStruct *>;
    set<SomeStruct *>::iterator it;
    SomeStruct *tmp;

    for (int i = 0 ; i < 1000; i ++)
    {
        tmp = new SomeStruct;
        tmp->a = i;
        some_cont->insert(tmp);
    }

    for (it = some_cont->begin(); it != some_cont->end(); it ++)
    {
        delete (*it);
    }

    some_cont->clear(); // <<<<THIS LINE
    delete some_cont;
    return 0;
}


在删除some_cont以避免内存泄漏之前是否需要调用“ THIS LINE”,否则将自动调用析构函数?

最佳答案

您不需要调用它,肯定会调用析构函数。

关于c++ - STL +内存管理问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6284231/

10-09 00:20