Dereference Adaptor

Discussion in 'C++' started by Matthias Kaeppler, Feb 27, 2005.

  1. Hello,

    I need to sort a range of pointers with a predicate which applies to the
    pointees. I tried to use boost::indirect_iterator, however, this will
    sort the container with the pointees instead the one with the pointers:

    vector<int> coll;
    // ...
    vector<int*> ptrcoll;
    // ...
    indirect_iterator< vector<int*>::iterator > begin(ptrcoll.begin()),
    end(ptrcoll.end());

    sort( begin, end ); // this sorts coll, not ptrcoll, what have I gained?

    Instead, is there some sort of adaptor, which allows things like this:

    vector<int> coll;
    // ...
    vector<int*> ptrcoll;
    // ...
    sort( ptrcoll.begin(), ptrcoll.end(), dereference( less<int>() ) );

    .... where dereference is an adaptor taking the function/functor
    representing the predicate on the pointees.

    --
    Matthias Kaeppler
    Matthias Kaeppler, Feb 27, 2005
    #1
    1. Advertising


  2. > I need to sort a range of pointers with a predicate which applies to the
    > pointees. I tried to use boost::indirect_iterator, however, this will sort
    > the container with the pointees instead the one with the pointers:
    >
    > vector<int> coll;
    > // ...
    > vector<int*> ptrcoll;
    > sort( ptrcoll.begin(), ptrcoll.end(), dereference( less<int>() ) );


    template<typename T> class DerefPtr {
    T *t_;
    public:
    DerefPtr(T *x) : t_(x) {}
    T operator *() const;
    };

    vector<Ptr<int> > ptrcoll;
    sort( ptrcoll.begin(), ptrcoll.end(), less<int>() );
    Kurt Krueckeberg, Feb 27, 2005
    #2
    1. Advertising

  3. Kurt Krueckeberg wrote:
    >>I need to sort a range of pointers with a predicate which applies to the
    >>pointees. I tried to use boost::indirect_iterator, however, this will sort
    >>the container with the pointees instead the one with the pointers:
    >>
    >>vector<int> coll;
    >>// ...
    >>vector<int*> ptrcoll;
    >>sort( ptrcoll.begin(), ptrcoll.end(), dereference( less<int>() ) );

    >
    >
    > template<typename T> class DerefPtr {
    > T *t_;
    > public:
    > DerefPtr(T *x) : t_(x) {}
    > T operator *() const;
    > };
    >
    > vector<Ptr<int> > ptrcoll;
    > sort( ptrcoll.begin(), ptrcoll.end(), less<int>() );
    >
    >


    And what is this supposed to do? ^^
    operator* has no body. And I guess vector<Ptr<int> > is supposed to be
    vector<DerefPtr<int> > ?

    --
    Matthias Kaeppler
    Matthias Kaeppler, Feb 27, 2005
    #3

  4. > And what is this supposed to do? ^^
    > operator* has no body. And I guess vector<Ptr<int> > is supposed to be
    > vector<DerefPtr<int> > ?
    >

    Sorry for the confusion.

    #include "stdafx.h"
    #include <vector>
    #include <functional>
    #include <algorithm>
    #include <iterator>
    using namespace std;

    // Comparison functor for use with associative containers
    struct DereferenceLess {
    public:
    template<typename PtrType> bool operator()(PtrType pT1, PtrType pT2)
    {
    return *pT1 < *pT2;
    }
    };

    struct Dereference {
    template<typename T> const T& operator()(const T* ptr) const
    {
    return *ptr;
    }
    };

    int main(int argc, char *argv[])
    {

    int *pi3 = new int(8);
    int *pi = new int(10);
    int *pi2 = new int(9);

    int *pi4 = new int(7);

    vector<int* > vec;
    vec.push_back(pi);
    vec.push_back(pi2);
    vec.push_back(pi3);
    vec.push_back(pi4);
    sort(vec.begin(), vec.end(), DereferenceLess());
    transform(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n"),
    Dereference() );

    set<int *, DereferenceLess> s;
    s.insert(pi1);
    s.insert(pi2);
    s.insert(pi3);
    s.insert(pi4);

    transform(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n"),
    Dereference() );


    return 0;
    }
    Kurt Krueckeberg, Feb 28, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S2VudCBL?=

    Creating a SQL adaptor object on a remote server

    =?Utf-8?B?S2VudCBL?=, Apr 1, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    319
    =?Utf-8?B?S2VudCBL?=
    Apr 1, 2004
  2. Sripathy Ramesh
    Replies:
    0
    Views:
    545
    Sripathy Ramesh
    Nov 25, 2003
  3. Scott Smedley

    A different kind of adaptor iterator

    Scott Smedley, Dec 5, 2003, in forum: C++
    Replies:
    4
    Views:
    405
    Scott Smedley
    Dec 7, 2003
  4. Stanislaw Salik

    pointer to reference adaptor

    Stanislaw Salik, Jul 30, 2004, in forum: C++
    Replies:
    3
    Views:
    715
    Stanislaw Salik
    Jul 30, 2004
  5. Matthias

    Dereferencing adaptor?

    Matthias, Feb 7, 2005, in forum: C++
    Replies:
    2
    Views:
    312
    Matthias
    Feb 8, 2005
Loading...

Share This Page