Christian Chrismann wrote:
Is my assumption correct?
Yes
Usually one want to compare
the objects (and not their addresses). To achieve this, one must
dereference ptrElement2 in the 'find' algorithm and additionally make sure
that ClassA provides a proper operator==. Right?
Yes, one way of doing this is to use find_if and provide a function as
the third argument defined as:
bool comp(ClassA const* lhs, ClassA const* rhs) { return *lhs == *rhs; }
Another one is to make your ClassA (cheaply) copyable:
class ClassA
{
public:
// ctor, functions, but no data members, which all live in impl
operator ==(ClassA const& other);
private:
boost::shared_ptr< Impl impl;
};
That way, it's cheap and feasible to use a vector of ClassAs directly
and thus you can search for it directly. Classes you use as types and
deal with very often are worth building that way.
Then there are some more bizarre alternatives, all of them require boost:
- boost::lambda
find_if(myVector.begin(), myVector.end(), *_1 == someClassA)
- boost::indirect_iterator
boost::indirect_iterator< ClassA const* >
indirect_first(myVector.begin()), indirect_last(myVector.end());
find(indirect_first, indirect_last, someClassA);
- boost::pointer_container
boost::ptr_vector< ClassA myPVector;
find(myPVector.begin(), myPVector.end(), someClassA);
I would use one of the first two solutions at first though.
Jens