Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr to object in stl set container?
From: Rob Desbois (rob.desbois_at_[hidden])
Date: 2011-12-13 11:28:09


n 13 December 2011 13:38, Lars Viklund <zao_at_[hidden]> wrote:

> On Tue, Dec 13, 2011 at 12:52:33PM +0000, Rob Desbois wrote:
> > On 13 December 2011 10:07, Igor R <boost.lists_at_[hidden]> wrote:
> >
> > > > -- I'm trying to use shared_ptr in a stl set, but am having trouble
> with
> > > find()
> > > > Any help? Documentation on this seems hard to find.
> > > >
> > > > -- Also, are the overloaded < and == useful or correct here?
> > >
> > >
> > > No, std algorithms won't call your comparison operators, because those
> > > of boost::shared_ptr do not compare pointees.
> > > You can define in your cpp something like this (operator ==() is not
> > > needed for your particular case):
> > >
> >
> > operator<() is needed for set sorting, but he does need operator==() for
> > the find() to work.
>
> Sets do not "need" op<. The default comparator template argument is
> std::less<K>, which has a fallback implementation using op< for things
> not explicitly specialized.
>

Apologies - what I meant included that, though was unclear.

> std::set<K>::find doesn't use op==. It uses the comparator in a fashion
> akin to !(key_comp(a,b) || key_comp(b,a)), that is, if neither key is
> less than the other, it's by necessity equivalent.
>

Ah, I didn't know that, though it makes sense thinking about it. Thanks :-)

-- 
Rob Desbois
Blog: http://theotherbranch.wordpress.com/
"I disapprove of what you say, but I’ll defend to the death your right to
say it", Voltaire


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