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 might change your accessor function to the simpler form
return unitCode == uCode;
You're better off looking for the position of the element rather than its index. Getting an element from its index is an O(n) operation, whereas getting an element from its position is an O(1) operation. So, with the STL and a little help from boost::bind():
#include <algorithm>
#include <boost/bind.hpp>
// ...
std::string uCode("uCode to search for");
std::list<Unit*>::iterator pos = std::find_if(unitCollection.begin(),
unitCollection.end(),
boost::bind(&Unit::isUnit,
_1, uCode));
The STL does have std::mem_fun(), which, along with std::bind2nd() would give the same result. The problem is that mem_fun() only works with member functions that take no arguments. boost::bind() on the other hand is much more powerful, and does solve the problem here very nicely. You should expect it in the next standard, which should be here immediately after the Messiah arrives.
If don't already have boost in your project then you really, really should install it. If the standard library is C++'s wife, then Boost is C++'s young lover. They should both be there, they get along fine.
Having said that, you can extract the function into a standalone function object as Peter mentioned already:
struct has_uCode {
has_uCode(cont std::string& uc) : uc(uc) { }
bool operator()(Unit* u) const { return u->isUnit(uc); }
private:
std::string uc;
};
Then, you can call std::find_if() like so:
std::list<Unit*>::iterator pos = std::find_if(unitCollection.begin(),
unitCollection.end(),
has_uCode("this and that"));
One more thing: I don't know how uCode's look like, but if they're big then you might speed things up by maintaing hashes of these strings so that in your search predicate you only compare the hashes. The hashes might be regular integers: comparing integers is pretty fast.
One more one-more-thing: If you run this search procedure often you might also consider changing your container type, because this really is an expensive procedure: in the order of the list's length.
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:
struct predicate
{
predicate( const std::string &uCode ) : uCode_(uCode) {}
bool operator() ( Unit *u )
{
return u->isUnit( uCode_ )
}
private:
std::string uCode_;
};
And then use it like this:
predicate pred("uCode");
std::list<Unit*>::iterator i;
i = std::find_if( UnitCollection.begin(), UnitCollection.end(), pred );
Or at least I think that would be a way to do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With