Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Dereference Adaptor

Reply
Thread Tools

Dereference Adaptor

 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      02-27-2005
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
 
Reply With Quote
 
 
 
 
Kurt Krueckeberg
Guest
Posts: n/a
 
      02-27-2005

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


 
Reply With Quote
 
 
 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      02-27-2005
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
 
Reply With Quote
 
Kurt Krueckeberg
Guest
Posts: n/a
 
      02-28-2005

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dereference precedence Toni C Programming 4 04-13-2006 09:02 PM
How does dereference operator overloading really work? Joe Seigh C++ 18 09-22-2003 05:42 PM
NULL Pointer Dereference Denis Palmeiro C Programming 10 07-16-2003 12:33 PM
Re: how to properly dereference STL list item Howard C++ 0 07-01-2003 05:46 PM
Re: how to properly dereference STL list item Jakob Bieling C++ 0 07-01-2003 05:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57