|
Boost : |
From: Giovanni Piero Deretta (gpderetta_at_[hidden])
Date: 2007-03-19 10:38:12
On 3/19/07, Lewis Hyatt <lhyatt_at_[hidden]> wrote:
> >
> > * provide an easy way to sort lists of pointers (without having to make
> > a custom '<' operator each time).
>
> this is already in boost:
>
> some_iterator begin, end;
> std::sort(boost::indirect_iterator(begin), boost::indirect_iterator(end));
>
or by using Boost.Lambda:
sort(begin, end, *_1 < *_2);
> * Easy way to specify different
> > comparison operators for various subfields. ie. in stead of one complex
> > '<' operator, specify one for each subfield and then define sortorder
> > (which subfield first, ...) on sorttime.
>
> This is an intriguing possibility, what about some intrusive design, where you
> can tag class members as sort keys?
>
Or a simple lambda again?
sort(begin, end, bind(&some_type::some_field, _1) <
bind(&some_type::some_field, _2));
This also automagically handles containers of (potentially smart) pointers.
With recent boost versions, even plain bind can be used with
relational operators.
You can also use this syntax with containers of pointers
sort(begin, end, _1->*&some_type::some_field) <
_2->*&some_type::some_field);
With containers to plain types you have to do,
sort(begin, end, (&_1)->*&some_type::some_field) <
(&_2)->*&some_type::some_field);
While ->* is almost idea, I tend to use an explicit bind.
gpd
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk