c++ - remove_if 后释放内存

标签 c++ stl

在下面的示例中,我从列表中删除了 pr2 对其应用返回 true 的范围内的一些元素。

m_list.remove_if(pr2(*tmp_list));

在我看来有必要删除上面删除的这个对象,因为当我创建它时我使用“new”(new CRectangle())。我该怎么做?我不知道在 remove_if 之后将删除哪些(以及多少)元素。

// test_cconnection.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

class CDrawObject
{
public:
    virtual ~CDrawObject()
    {
        cout << "Drop CDrawObject: " << id_ << endl;
    }
    int getId() const
    {
        return id_;
    }
    virtual void draw()
    {
    }
protected:
    static int id;
    int id_;
};

class CRectangle : public CDrawObject
{
public:
    CRectangle()
    {
        id_ = id++;
    }
    ~CRectangle()
    {
        cout << "Drop CRectangle: " << id_ << endl;
    }
    virtual void draw()
    {
        cout << "CRectangle, id: " << id_ << endl;
    }
};

class CMarker : public CDrawObject
{
    CDrawObject* obj;
public:
    CMarker(CDrawObject* obj_)
    {
        obj = obj_;     
    }
    ~CMarker()
    {
        cout << "Delete marker of object with id: " << obj->getId() << endl;
    }
    CDrawObject* getObject() const
    {
        return obj;
    }
    virtual void draw()
    {
        cout << "CMarker of oject with id: " << obj->getId() << endl;
    }
};

int CDrawObject::id = 0;

// predicate for compare objects with int id
class pr : public std::unary_function<CDrawObject*, bool>
{
private:
    int id_; 
public: 
    pr(int id): id_(id) {}  
    bool operator()(CDrawObject* arg) const 
    { 
        return (arg->getId() == id_); 
    } 
};

// predicate for check objects with type CMarker and
// compare with CDrawObject* obj
class pr2 : public std::unary_function<CDrawObject*, bool>
{
private:
    CDrawObject* obj_; 
public: 
    pr2(CDrawObject* obj)
    {
        obj_ = obj;
    } 
    bool operator()(CDrawObject* arg) const 
    { 
        if (dynamic_cast<CMarker*>(arg))
            return ((dynamic_cast<CMarker*>(arg))->getObject() == obj_); 
    } 
};

int _tmain(int argc, _TCHAR* argv[])
{
    list<CDrawObject*> m_list;
    list<CDrawObject*>::iterator i_list, tmp_list;

    m_list.push_back(new CRectangle());
    tmp_list = m_list.end();
    m_list.push_back(new CMarker(*--tmp_list));
    m_list.push_back(new CMarker(*tmp_list));

    m_list.push_back(new CRectangle());
    tmp_list = m_list.end();
    m_list.push_back(new CMarker(*--tmp_list));

    m_list.push_back(new CRectangle());
    tmp_list = m_list.end();
    m_list.push_back(new CMarker(*--tmp_list));
    m_list.push_back(new CMarker(*tmp_list));

    // print on screen items of m_list
    for (i_list = m_list.begin(); i_list != m_list.end(); ++i_list)
        (*i_list)->draw();

    // get an iterator to the first element in the range with id_ = 2
    tmp_list = find_if(m_list.begin(), m_list.end(), pr(2));

    if (tmp_list !=  m_list.end())
    {
        // remove from list all elements with type CMarker 
        // and CDrawObject = tmp_list       
        m_list.remove_if(pr2(*tmp_list));
    }

    cout << endl << "--------" << endl;

    // print on screen items of m_list
    for (i_list = m_list.begin(); i_list != m_list.end(); ++i_list)
     (*i_list)->draw();


    _getch();
    return 0;
}

最佳答案

你可以:

HACKISH:删除谓词中的对象。

烦人:远离 remove_if 并自行实现它所做的一切,除了添加删除。

更好:使用 RAII 对象而不是原始指针。换句话说,某种智能指针。

关于c++ - remove_if 后释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4686538/

相关文章:

c++ - 模板和静态变量

c++ - Objective-C++ 错误 : Expected unqualified-id

c++ - C++中根据字符串创建对象

c++ - 对 std::list 进行排序时,运算符 < 无效

c++ - 插入一个 std::list 元素到 std::list

c++ - 希望在 STL vector 中找到 C++ STL vector

c++ - constexpr 和 virtual

c++ - 在 Qt5 的 QQuickItem 上捕捉 mouseMoveEvent

c++ - STL集查找性能

c++ - 创建 std::pair<A,B> 时出现“operator<”错误