Dereference Adaptor

M

Matthias Kaeppler

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.
 
K

Kurt Krueckeberg

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

Matthias Kaeppler

Kurt said:
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> > ?
 
K

Kurt Krueckeberg

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top