Boost logo

Boost Users :

From: Johan Ericsson (johanericsson_at_[hidden])
Date: 2002-04-29 23:45:10


Use std::find_if, and pass it a predicate function or
function object. That is the standard way that you
override the equality function if it doesn't do what
you want.

http://www.sgi.com/tech/stl/find_if.html

This function object should work nicely:

// warning unchecked code
template<class T>
struct DerefCompare
{
   bool operator()(boost::shared_ptr<T> t) const
   {
     return ((*t)==val);
   }
   DerefCompare(T inVal):val(inVal){}
   T val;
};

template <class T>
DerefCompare<T> make_deref(T t)
{
    return DerefCompare<T>(t);
}

Then to use it, use syntax like:

std::find_if(vec.begin(),vec.end(),make_deref(5));

Johan
--- sashan <sashang_at_[hidden]> wrote:
> Hi
>
> Because the operator== function compares pointers
> and not what they're pointing to this is what I've
> had to do to get it to find an integer. Basically I
> had to wrap the basic int type within another class
> called Int, as show below.
>
> class Int
> {
> private:
> shared_ptr<int> p;
>
> public:
> Int(int i) {p.reset(new int); *p = i;}
> bool operator==(const Int& r) {return *p.get() ==
> *r.p.get();}
> ~Int() {};
> };
>
> void main()
> {
> vector < Int > vec;
> vec.push_back(Int(1));
> vec.push_back(Int(2));
> vec.push_back(Int(3));
> Int p(4);
> if (find(vec.begin(), vec.end(), p) == vec.end())
> {
> cout << "Couldn't find it";
> }
> }
>
> Is there a better way that doesn't involve 1) having
> to write a wrapper class around a basic type (or any
> type) and 2) doesn't involve changing the
> shared_ptr::operator==.
>
>
>
> [Non-text portions of this message have been
> removed]
>
>
>

__________________________________________________
Do You Yahoo!?
Yahoo! Health - your guide to health and wellness
http://health.yahoo.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net