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?