0

I've tried removing the elemetns (pointers to a named type) from a list using std::remove (I've read about this algorithm here and here) and a combination of list::erase and std::find.

Here's the code that I wrote for this purpose:

#include <iostream>

#include <list>
#include <functional>
#include <string>
#include <algorithm>


class NamedType
{
    std::string name_; 

    public: 
        NamedType (const char* name)
            : 
                name_(name)
        {}    

        void info()
        {
            std::cout << name_ << ": NamedType::update()" << std::endl;
        }
};

class NamedTypeList
{
    std::list<NamedType*> objectList_; 

    public: 

        void addNamedType(NamedType& o)
        {
            NamedType* oPtr = &o; 
            objectList_.push_back(oPtr);
        }

        void removeNamedTypeWithFind(NamedType& o)
        {
            std::list<NamedType*>::iterator removedNamedType = std::find(
                objectList_.begin(), objectList_.end(), &o);

            if (removedNamedType != objectList_.end())
            {
                objectList_.erase(removedNamedType); 
            }
        }

        void removeNamedType(NamedType& o)
        {
            std::remove(objectList_.begin(), objectList_.end(), &o); 
        }

        void namedObjectsInfo()
        {
            std::for_each(objectList_.begin(), objectList_.end(), 
                std::mem_fun(&NamedType::info)); 
        }
};

using namespace std;

int main ()
{
    NamedType o1("o1"); 
    NamedType o2("o2"); 
    NamedType o3("o3"); 
    NamedType o4("o4"); 

    NamedTypeList objectList1; 
    NamedTypeList objectList2; 

    objectList1.addNamedType(o1); 
    objectList1.addNamedType(o2); 
    objectList1.addNamedType(o3); 
    objectList1.addNamedType(o4); 

    objectList2.addNamedType(o1); 
    objectList2.addNamedType(o2); 
    objectList2.addNamedType(o3); 
    objectList2.addNamedType(o4); 

    cout << "Registered objects into objectList1:" << endl;
    objectList1.namedObjectsInfo(); 

    cout << "Registered objects into objectList2:" << endl;
    objectList2.namedObjectsInfo(); 

    cout << "Removing o2 object from objectList1 with remove" << endl;

    objectList1.removeNamedType(o2); 
    objectList1.namedObjectsInfo(); 


    cout << "Removing o2 object from objectList2 with std::find" << endl;

    objectList2.removeNamedTypeWithFind(o2); 
    objectList2.namedObjectsInfo(); 

}; 

What I don't get is why I get the following output when I call objectList1.removeNamedType(o2); :

Removing o2 object from objectList1 with remove
o1: NamedType::update()
o3: NamedType::update()
o4: NamedType::update()
o4: NamedType::update()

I'm having trouble understanding the documentation: I get that there is a new_end iterator showing the new end of the range, but this does not work then if I have multiple same NamedTypes. E.g. if I register the object o2 twice in the objectList1, it will be visible and its member function will be called by the namedObjectsInfo() method since it loops over all elements (it doesn't see the new_end iterator).

If I understood it right, should I even use std::remove to remove elements from a container, or a combination of std::find and list::erase in this case?

4

3 回答 3

5

当容器有自己的remove方法时std::list,你应该使用它而不是std::remove. 对于没有remove方法的容器,您应该使用此处其他答案中描述的擦除删除习语。

于 2012-12-26T19:53:36.597 回答
3

std::remove不会更新容器结构本身,因此您最终会在迭代器范围的末尾出现“无关”元素。为了彻底清理容器,你需要结合 remove 和 erase:

    void removeNamedType(NamedType& o)
    {
        objectList_.erase(std::remove(objectList_.begin(), objectList_.end(), &o), objectList_.end()); 
    }

但是,正如 IronMensan 的回答中提到的,更好的方法是std::list::remove()在您的情况下使用它,因为它针对列表上的操作进行了优化并摆脱了多阶段删除。

于 2012-12-26T19:48:59.390 回答
1

您必须同时使用两者removeerase因为remove不会通知您的容器它已取出元素(它仍会认为它包含 4 个对象,因此您最终会遇到奇怪的事情)。

void removeNamedType(NamedType& o)
{
    objectList_.erase(
        std::remove(objectList_.begin(), objectList_.end(), &o),
        objectList_.end()); 
}
于 2012-12-26T19:52:19.310 回答