Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-02-28 09:11:51


Matthias Kaeppler <nospam_at_[hidden]> writes:

> Consider this code:
>
> vector<int> coll;
> // ... push back some values
> vector<int*> ptrcoll;
> // ... push back pointers to the values of coll
>
> indirect_iterator< vector<int*>::iterator > begin(ptrcoll.begin()),

Careful; vector iterators are not neccessarily pointers.

> end(ptrcoll.end());
>
> sort( begin, end, less<int> );
>
> --
>
> My question: After the sort statement, coll is sorted, right? (and
> ptrcoll is not). However, I need to sort a vector of pointers according
> to a predicate which applies to non-pointers. In this case, I want to
> sort ptrcoll, according to the less-relation on the pointees in coll.
>
> How can I achieve that?

struct indirect_less
{
   template <class It>
   bool operator()(It i1, It i2) const
   {
       typedef typename std::iterator_traits<It>::value_type value_type;
       return std::less<value_type>()(i1,i2);
   }
};

sort(ptrcoll.begin(), ptrcoll.end, indirect_less());

HTH,

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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net