Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-04-24 05:23:31


HEATH DAVIS wrote:
> I have experienced some difficulty with sorting lists of type:
>
> list < shared_ptr < foo > >
>
> [snip]
>
> bool my_class::operator<(const my_class& arg)
> {
> return m_val < arg.m_val;
> }
>
> [snip]
>
> //sort the list
> my_list.sort();

Here, you basically sort by using

shared_ptr::operator<

instead of

my_class::operator<

You need something like

template< T > struct shared_ptr_content_less
{
   bool operator()( const boost::shared_ptr< T >& lhs,
                    const boost::shared_ptr< T >& rhs ) const
   {
     return *lhs < *rhs;
   }
};

and call

my_list.sort( shared_ptr_content_less< my_list >() );

The code is untested, but I hope you get the idea. Also, there might be
some easier way using other boost-libs to create the helper on-the-fly,
but others definitely know better than me :)

Regards, Daniel

-- 
Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk