Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-03-01 13:02:28


Matthias Kaeppler <nospam_at_[hidden]> writes:

> David Abrahams wrote:
>> Careful; vector iterators are not neccessarily pointers.
>
> Hm yes, that's true. But does this matter? I have a vector of some type,
> and a vector of pointers to some type. I don't even deal with iterators
> when working on the pointer-vector.

Sorry, I read your post too quickly and jumped to the wrong
conclusion.

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

Whoops; I meant:
          return std::less<value_type>()(*i1,*i2);
>> }
>> };
>> sort(ptrcoll.begin(), ptrcoll.end, indirect_less());
>>
>
> Ah I see. But what, if I want to abstract from the predicate? I want to
> create a class which is as generic as possible, so I don't have to
> rewrite it for each and every possible predicate.

I take it from reading this message and your other one that you mean
you want to write an adapter for any binary predicate on T that will
instead take arguments that can be dereferenced to get Ts and forward
those Ts on to the underlying binary predicate (but that's not really
clear)?

  template <class Pred>
  struct indirector
  {
      indirector(Pred const& p) : p(p) {}

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

      Pred p;
  };

  template <class Pred>
  indirector<Pred> indirect(Pred const& p)
  {
       return indirector(p);
  }

  sort(ptrcoll.begin(), ptrcoll.end, indirect(less<int>()));

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