473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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<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
Jul 23 '05 #1
3 2448
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>() );
Jul 23 '05 #2
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
Jul 23 '05 #3
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;
}

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: FrodoBaggins | last post by:
Dear Team, I have set up a Windows Server 2003 on a laptop PC in order to experiment with Visual Studio .NET. On attempting to set up Active Directory, I get the message: "The wizard has...
3
by: Stanislaw Salik | last post by:
Hi, Lets suppose we want to use generic algotithms on collections of pointers (both raw and smart). For example, we want to sort a vector of (smart)pointers. We need a comparator that will take...
15
by: Matthias Kaeppler | last post by:
Hi, as a result of my last posting about an adaption class template for invoking algorithms on a container of iterators or pointers to elements in another container, I have come up with this...
1
by: Tony Johansson | last post by:
Hello! I'm reading about design pattern adaptor in the GOF book and there is something that sounds strange. When you use the adaptor design pattern you have these participants. *Target -...
0
by: Tony Johansson | last post by:
Hello! Below is a small program using the adaptor pattern. We have four classes these are: PegAdapter, RoundPeg, SquarePeg and a class TestPegs acting like a client In class PegAdapter has...
0
by: Tony Johansson | last post by:
Hello!! This is about the adaptor design pattern. If you use the class adaptor its easy to override. It says an object adaptor makes it harder to override Adaptee behavior. It will require...
1
by: | last post by:
Greetings All, I'm trying to access a excel file using the odbc data adaptor but the tables arent showing up. I can get connected to the excel file using the Wizard but when I go to do the odbc...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
26
by: =?iso-8859-1?q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Do you think we can reach any kind of consensus on whether the following code's behaviour is undefined by the Standard? int my_array; int const *const pend = *(&my_array + 1); Considering...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.