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

Home Posts Topics Members FAQ

pointer to reference adaptor

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.beg in(), v.end(), std::less<int>( ));

The code above will noc compile, since std::less<std:: string>() object
takes two strings, while we have pointers to std::string. So we need an
adaptor.

template <class T, class U>
struct pointer_to_ref
{
typedef T argument_type;
typedef U result_type;

inline
result_type & operator() (argument_type obj)
{
return *obj;
}
};

std::vector<int *> v;
std::sort(v.beg in(), v.end(),
boost::bind(std ::less<string>( ),
boost::bind (pointer_to_ref <std::string* , std::string>(), _1),
boost::bind (pointer_to_ref <std::string* , std::string>(), _2)));

And the code will just work fine.

And here goes my question. Since there are so many places where
pointer_to_ref (and pointer_to_valu e probably) would be useful, is there
any standard class that does the same thing?

For example, for smart pointers i can use
&collection_typ e::value_type:: operator* as an adaptor. But what if i
wanted to make code more generic and working with raw pointers too?

Regards,
Stanislaw

Jul 22 '05 #1
3 2269

"Stanislaw Salik" <s.*****@microg en.pl> wrote in message
news:ce******** **@news.onet.pl ...
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.
<snip>
template <class T, class U>
struct pointer_to_ref
{
typedef T argument_type;
typedef U result_type;

inline
result_type & operator() (argument_type obj)
{
return *obj;
}
}; And here goes my question. Since there are so many places where
pointer_to_ref (and pointer_to_valu e probably) would be useful, is there any standard class that does the same thing?


First, for the example you gave -- which is a common situation -- you
should be able to use indirect_iterat ors
(http://www.boost.org/libs/iterator/d..._iterator.html)

Second, I don't think it's necessary to have two template parameters.
You should be able to calculate the result type given the argument
type (see http://www.boost.org/boost/pointee.hpp.)

Jonathan
Jul 22 '05 #2
First, for the example you gave -- which is a common situation -- you
should be able to use indirect_iterat ors
(http://www.boost.org/libs/iterator/d..._iterator.html)

Second, I don't think it's necessary to have two template parameters.
You should be able to calculate the result type given the argument
type (see http://www.boost.org/boost/pointee.hpp.)


Thanks a lot!
Jul 22 '05 #3
Thanks for the information.

Jonathan Turkanis wrote:
First, for the example you gave -- which is a common situation -- you
should be able to use indirect_iterat ors
(http://www.boost.org/libs/iterator/d..._iterator.html)
The indirect_iterat or adaptor works fine. However, the resulting
algorithm is a bit different. With indirect_iterat or, std::sort works on
objects (in this case integers), while the approach with
pointer_to_refe rence adaptors works on (smart)pointers .

In the example1.cpp below outputs

vect: 8 0 5 9 1 6 3 4 7 2
copy: 0 1 2 3 4 5 6 7 8 9
vect: 0 1 2 3 4 5 6 7 8 9
copy: 0 1 2 3 4 5 6 7 8 9
vect: 6 1 7 4 2 0 8 5 9 3
copy: 0 1 2 3 4 5 6 7 8 9
vect: 0 1 2 3 4 5 6 7 8 9
copy: 5 1 4 9 3 7 0 2 6 8

Notice that the copy becomes permuted while the original was sorted.

Is there a way to use indirect_iterat or in such way that sort replaces
pointers instead of pointed values?

Jonathan Turkanis wrote:
Second, I don't think it's necessary to have two template parameters.
You should be able to calculate the result type given the argument
type (see http://www.boost.org/boost/pointee.hpp.)


This makes the code a bit cleaner. Is there a way to create adaptor
object straight from the pointee<> template? Or is it still necessary to
declare structure that does the trick?

Regards,
Stanislaw

example1.cpp

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

#include <stdlib.h>

#include <boost/iterator/indirect_iterat or.hpp>
#include <boost/bind.hpp>

template <class T>
struct pointer_to_ref
{
typedef T argument_type;
typedef typename boost::pointee< T>::type & result_type;

inline
result_type operator() (argument_type obj)
{
return *obj;
}
};

struct counter
{
typedef int* result_type;

counter () :n (0) {}
result_type operator() () { return new int (n++); }

int n;
};

typedef std::vector<int *> vector_t;
int
main ()
{
srand(time(0));

vector_t v;

std::generate_n (std::back_inse rt_iterator<vec tor_t>(v), 10,
counter());

vector_t w (v);

std::random_shu ffle (v.begin(), v.end());

std::cout << "vect: ";
std::copy (boost::make_in direct_iterator (v.begin()),
boost::make_ind irect_iterator( v.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

std::cout << "copy: ";
std::copy (boost::make_in direct_iterator (w.begin()),
boost::make_ind irect_iterator( w.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

std::sort (v.begin(),
v.end(),
boost::bind(std ::less<int>(),

boost::bind(poi nter_to_ref<vec tor_t::value_ty pe>(), _1),

boost::bind(poi nter_to_ref<vec tor_t::value_ty pe>(), _2)));

std::cout << "vect: ";
std::copy (boost::make_in direct_iterator (v.begin()),
boost::make_ind irect_iterator( v.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

std::cout << "copy: ";
std::copy (boost::make_in direct_iterator (w.begin()),
boost::make_ind irect_iterator( w.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

return 0;
}

example2.cpp:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

#include <stdlib.h>

#include <boost/iterator/indirect_iterat or.hpp>
struct counter
{
typedef int* result_type;

counter () :n (0) {}
result_type operator() () { return new int (n++); }

int n;

};

typedef std::vector<int *> vector_t;

int
main ()
{
srand(time(0));

vector_t v;

std::generate_n (std::back_inse rt_iterator<vec tor_t>(v), 10,
counter());

vector_t w (v);

std::random_shu ffle (v.begin(), v.end());

std::cout << "vect: ";
std::copy (boost::make_in direct_iterator (v.begin()),
boost::make_ind irect_iterator( v.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

std::cout << "copy: ";
std::copy (boost::make_in direct_iterator (w.begin()),
boost::make_ind irect_iterator( w.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

std::sort (boost::make_in direct_iterator (v.begin()),
boost::make_ind irect_iterator( v.end()),
std::less<int>( ));

std::cout << "vect: ";
std::copy (boost::make_in direct_iterator (v.begin()),
boost::make_ind irect_iterator( v.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

std::cout << "copy: ";
std::copy (boost::make_in direct_iterator (w.begin()),
boost::make_ind irect_iterator( w.end()),
std::ostream_it erator<int>(std ::cout, " "));
std::cout << std::endl;

return 0;
}
Jul 22 '05 #4

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

Similar topics

3
2484
by: Matthias Kaeppler | last post by:
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; // ...
110
9969
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
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
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
2483
by: HIK | last post by:
I am trying to write a class which has a function that loads a dataset, and performs and xslt tranformtion on the dataset and returns the resulting string. In asp.net 1.1 I was able to do most of the work with the designer. 1. Create a dataadapter in the designer 2. generate a dataset 3. Create a proceedure that would reference the dataset. Fill it, convert the dataset to an xmldatadocument and then transform the xmldatdocument...
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
13
2684
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
5
2271
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
7
2635
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a different kind of struct
0
10449
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
10217
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...
0
9047
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
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.