Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-10-11 07:12:34


"Thorsten Ottosen" <nesotto_at_[hidden]> writes:

> 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() );

That is also the correct solution.

There's a way to get the effect you want with the new iterator
adaptors using a proxy reference type, provided, of course, that your
stdlib's sort doesn't check to make sure it has an lvalue iterator.
Its harder and probably less efficient than the above.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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