我尝试使用std :: remove从列表中删除elemetns(指向命名类型的指针)(我在这里和这里读过这个算法)以及list :: erase和std :: find的组合.
这是我为此目的编写的代码:
#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();
};
Run Code Online (Sandbox Code Playgroud)
我没有得到的是为什么当我调用objectList1.removeNamedType(o2)时我得到以下输出; :
Removing o2 object from objectList1 with remove
o1: NamedType::update()
o3: NamedType::update()
o4: NamedType::update()
o4: NamedType::update()
Run Code Online (Sandbox Code Playgroud)
我无法理解文档:我知道有一个new_end迭代器显示范围的新结束,但是如果我有多个相同的NamedTypes,那么这不起作用.例如,如果我在objectList1中两次注册对象o2,它将是可见的,并且其成员函数将由namedObjectsInfo()方法调用,因为它遍历所有元素(它没有看到new_end迭代器).
如果我理解正确,我是否应该使用std :: remove从容器中删除元素,或者在这种情况下使用std :: find和list :: erase的组合?
当容器有自己的remove方法时std::list,你应该使用它而不是std::remove.对于没有remove方法的容器,您应该使用此处其他答案中描述的擦除 - 删除习惯用法.