Boost logo

Boost :

From: Alex Chovanec (achovane_at_[hidden])
Date: 2003-10-11 20:39:33


"Thorsten Ottosen" <nesotto_at_[hidden]> wrote in message
news:bm8j1m$4cv$1_at_sea.gmane.org...
> "Brian McNamara" <lorgon_at_[hidden]> wrote in message
> news:20031011032816.A12603_at_maui-lnx.cc.gatech.edu...
>
> Brian, thanks for your cat and dog example; the thing is that it could be
> necessary to sort polymorphic types or that Base is not polymophic.
Anyway,
> the hard thing is to make the copy copy pointers and not objects. The easy
> solution
> (which I'm leaning a little towards ) is simply to provide normal
iterators:
>
> struct ptr_less_than
> {
> template< typename T >
> bool operator()( const T*l, const T* r)
> {
> *l < *r;
> }
> };
>
> sort( v.ptr_begin(), v.ptr_end(), ptr_less_than() );

Thorsten,
One disadvantage of this approach is that it requires you to define
specialized function objects which act on pointers. If your operator*()
returns some sort of proxy, as you originally suggested, you can provide it
with an implicit cast to const T &. This will allow you to sort the
ptr_vector without defining ptr_less_than. You can simply use std::less<T>.

sort(v.ptr_begin(), v.ptr_end(), std::less<T>());

Alex


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