 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthias Kaeppler Guest
|
Posted: Sun Feb 27, 2005 6:55 pm Post subject: Dereference Adaptor |
|
|
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 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
|
|
| Back to top |
|
 |
Kurt Krueckeberg Guest
|
Posted: Sun Feb 27, 2005 8:36 pm Post subject: Re: Dereference Adaptor |
|
|
| Quote: | 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 ptrcoll;
sort( ptrcoll.begin(), ptrcoll.end(), less<int>() );
|
|
| Back to top |
|
 |
Matthias Kaeppler Guest
|
Posted: Sun Feb 27, 2005 8:51 pm Post subject: Re: Dereference Adaptor |
|
|
Kurt Krueckeberg wrote:
| Quote: | 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 ptrcoll;
sort( ptrcoll.begin(), ptrcoll.end(), less<int>() );
|
And what is this supposed to do? ^^
operator* has no body. And I guess vector<Ptr is supposed to be
vector<DerefPtr ?
--
Matthias Kaeppler
|
|
| Back to top |
|
 |
Kurt Krueckeberg Guest
|
Posted: Mon Feb 28, 2005 5:42 pm Post subject: Re: Dereference Adaptor |
|
|
| Quote: | And what is this supposed to do? ^^
operator* has no body. And I guess vector<Ptr is supposed to be
vector<DerefPtr ?
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
{
return *ptr;
}
};
int main(int argc, char *argv[])
{
int *pi3 = new int( ;
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;
}
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|