473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_iterat or< vector<int*>::i terator > begin(ptrcoll.b egin()),
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 2484
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<typena me 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<typena me 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<typena me PtrType> bool operator()(PtrT ype pT1, PtrType pT2)
{
return *pT1 < *pT2;
}
};

struct Dereference {
template<typena me T> const T& operator()(cons t 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(p i);
vec.push_back(p i2);
vec.push_back(p i3);
vec.push_back(p i4);
sort(vec.begin( ), vec.end(), DereferenceLess ());
transform(vec.b egin(), vec.end(), ostream_iterato r<int>(cout, "\n"),
Dereference() );

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

transform(vec.b egin(), vec.end(), ostream_iterato r<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
4380
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 detected that there are no valid IP addresses assigned to any of the network adapters for this computer. Please verify that the network cable is connected, and that a DHCP server is available or the
3
2270
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 two pointers and return a bool. std::vector<std::string *> v; std::sort(v.begin(), v.end(), std::less<int>());
15
1606
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 code: #include <vector> #include <functional> #include <algorithm> #include <iostream>
1
3302
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 - defines the domain-specific interface that Client uses. * Client
0
2710
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 some text just above the class header saying "Its adapts a RoundPeg to a SquarePeg". In the class that acts as a client which isTestPegs
0
2595
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 subclassing Adaptee and making Adapter refer to the subclass rather then the Adaptee itself. What does this mean? Can you give some easy exaples if you have some?
1
1774
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 adaptor no tables show up in the query wizard why is this? TIA
6
5423
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, vectors, operator overloading and what not.. And i was wondering if there is a way to override the global dereference operator, so to be able to check the address that one tries to dereference. This gives the ability to throw an exception when...
26
4522
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 the syntax of the language, then we definitely do dereference an invalid pointer... but if we consider the mechanics of the language, then we know that nothing "happens" when we dereference a pointer
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.