Boost logo

Boost :

From: Matthew Towler (towler_at_[hidden])
Date: 2003-04-24 06:01:07


> I have experienced some difficulty with sorting lists of type:
>
> list < shared_ptr < foo > >
>
> Here is the expected (sorted) program output:
>
> 424238335
> 596516649
> 719885386
> 846930886
> 1189641421
> 1649760492
> 1681692777
> 1714636915
> 1804289383
> 1957747793
>
> Instead, I end up with:
>
> 1804289383
> 846930886
> 1681692777
> 1714636915
> 1957747793
> 424238335
> 719885386
> 1649760492
> 596516649
> 1189641421
>
>
>
> list < shared_ptr < my_class > > my_list;
> list < shared_ptr < my_class > >::iterator my_iter;
>
The problem is here, as your list contains shared_ptr < my_class >, the
list is ordered by shared_ptr < my_class >::operator< which will usually
amount to an address comparison of the pointers contained in the shared_ptr.

To fix this you need to pass in my_class::operator< as the predicate to
use for the sort().

Matt


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