Boost logo

Boost Users :

Subject: Re: [Boost-users] Using boost::shared_ptr with std::set
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-04-04 16:30:29


> From: przemyslaw.sliwa_at_[hidden]
>
> <snip>
>
> In order to sort the objects in the set I have to use the functor
>
> struct lessThan : public
> std::binary_function,
> boost::shared_ptr, bool>
> {
> bool operator ()(const boost::shared_ptr
> &lhs, const boost::shared_ptr &rhs) const
> {
> return (*lhs) < (*rhs);
> }
> };
>
> This is quite long and I believe boost can shorten it a lot. I have
> tried it with boost::lamba and boost::bind but could not make it
> working. Basically I would like to eliminate the need of writing the
> definition of struct lessThan. Could someone help me with that?
>

You could write one class that works for all types:
 
struct indirectLessThan
{
    template <typename PtrT>
    bool operator()(PtrT a, PtrT b)
    {
        return *a < *b;
    }
};

And use it like this:
 
typedef std::set<eventIntLimPtr, indirectLessThan> eventIntLimPtrSet;

And you can re-use this indirectLessThan class in other situtations where
you want to compare pointers (whether shared_ptr or raw pointers)
by comparing the things they point to.
 
Regards,
Nate.
                                                


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