I have a list:
list<Unit *> UnitCollection;
containing Unit objects, which has an accessor like:
bool Unit::isUnit(string uCode)
{
if(this->unitCode == uCode)
return true;
else
return false;
}
How do I search my UnitCollection list by uCode and return the corresponding element (preferably and iterator).
In pseudo code it would look something like this:
for every item in my UnitCollection:
if the unit.isUnit(someUnitIpass)
do something
else
next unit
I have looked at the find() method, but i’m not sure you can pass a boolean method in instead of a searched item parameter if that makes sense.
You could have a look at find_if as jpalecek suggests, and then use distance to find the distance between the iterator returned from find_if and UnitCollection.begin(), and that distance should be the index of the element in the list.
And as for the predicate, you could write a function object like this:
And then use it like this:
Or at least I think that would be a way to do it.