473,402 Members | 2,053 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,402 software developers and data experts.

How to write the transform equivalent in STL?

I have the following code snippet:

class converter
{
// ...
public:
virtual polar_point convert_to_polar(const point&) = 0;
};

void f(const std::vector<point>& points, converter& cvt)
{
vector<polar_pointpolarpoints(points.size());

for (unsigned int i = 0; i < points.size(); ++i)
{
polarpoints[i] = cvt.convert(points[i]);
}

print_points(polarpoints);

// ...
}
I'd like to replace the for loop with an appropriate std::transform
call. But I can't figure out what functor should I pass to transform...I
am thinking a number of combinations of mem_fun and bind1st/bind2nd, but
they don't seem to work.

Any ideas?

Ben
Aug 30 '06 #1
4 1853
benben <benhonghatgmaildotcom@nospamwrote:
I have the following code snippet:

class converter
{
// ...
public:
virtual polar_point convert_to_polar(const point&) = 0;
};

void f(const std::vector<point>& points, converter& cvt)
{
vector<polar_pointpolarpoints(points.size());

for (unsigned int i = 0; i < points.size(); ++i)
{
polarpoints[i] = cvt.convert(points[i]);
}

print_points(polarpoints);

// ...
}
I'd like to replace the for loop with an appropriate std::transform
call. But I can't figure out what functor should I pass to transform...I
am thinking a number of combinations of mem_fun and bind1st/bind2nd, but
they don't seem to work.

Any ideas?
transform( points.begin(), points.end(), polarpoints.begin(),
bind1st( mem_fun( &converter::convert_to_polar ), &cvt ) );

However the above will only work if you change the converter...

class converter
{
// ...
public:
virtual polar_point convert_to_polar(point) = 0;
// parameter no longer a const point&
};

Note that:

transform( points.begin(), points.end(), polarpoints.begin(),
bind1st( mem_fun_ref( &converter::convert_to_polar ), cvt ) );

Won't work because converter is an ABC.
Aug 30 '06 #2
transform( points.begin(), points.end(), polarpoints.begin(),
bind1st( mem_fun( &converter::convert_to_polar ), &cvt ) );

Thanks, that worked!
>
However the above will only work if you change the converter...

class converter
{
// ...
public:
virtual polar_point convert_to_polar(point) = 0;
// parameter no longer a const point&
};
Hmm, can you elaborate a little bit more or point me to an article on
this. This bothers me because the calling to a function taking a const
reference or value should at least look the same at call site...perhaps
it is the parameter forwarding that causes this restriction...but anyway
I am not quite sure...more research to do.
>
Note that:

transform( points.begin(), points.end(), polarpoints.begin(),
bind1st( mem_fun_ref( &converter::convert_to_polar ), cvt ) );

Won't work because converter is an ABC.
Yup, I am using a specific converter function from a derived class.

Thank you!

Ben
Aug 30 '06 #3

benben wrote:
transform( points.begin(), points.end(), polarpoints.begin(),
bind1st( mem_fun( &converter::convert_to_polar ), &cvt ) );


Thanks, that worked!

However the above will only work if you change the converter...

class converter
{
// ...
public:
virtual polar_point convert_to_polar(point) = 0;
// parameter no longer a const point&
};

Hmm, can you elaborate a little bit more or point me to an article on
this. This bothers me because the calling to a function taking a const
reference or value should at least look the same at call site...perhaps
it is the parameter forwarding that causes this restriction...but anyway
I am not quite sure...more research to do.
bind1st and bind2nd add a reference to T, T being whatever type is the
parameter in the target function call. This reference is accepted as
the resulting functor's parameter. Essentially you have:

template <typename X, typename R, typename T>
struct binder2nd
{
X * x;
R operator()(T & t) { return x->functionName(t); }
};

Of course the actual definition isn't the above but that is the gist of
the problem. What happens then if your function accepts a reference is
that T is then a reference and so operator() in the binder accepts a
reference reference, which is not a legal type. The code then won't
compile.

The TR1 bind setup does better I believe.

Aug 30 '06 #4
In article <44**********************@news.optusnet.com.au>,
benben <benhonghatgmaildotcom@nospamwrote:
transform( points.begin(), points.end(), polarpoints.begin(),
bind1st( mem_fun( &converter::convert_to_polar ), &cvt ) );


Thanks, that worked!

However the above will only work if you change the converter...

class converter
{
// ...
public:
virtual polar_point convert_to_polar(point) = 0;
// parameter no longer a const point&
};

Hmm, can you elaborate a little bit more or point me to an article on
this. This bothers me because the calling to a function taking a const
reference or value should at least look the same at call site...perhaps
it is the parameter forwarding that causes this restriction...but anyway
I am not quite sure...more research to do.
As Noah already detailed. Making the parameter a reference will cause a
reference to reference type which isn't allowed.

Also, in this specific case, I'd more likely use generate_n instead of
transform.
Aug 31 '06 #5

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

Similar topics

2
by: John Lehmann | last post by:
I have an interesting problem. I am performing an XSL transform using the System.Xml.Xsl.Transform class. I have a database that contains the XSL style sheet string. And it seems to work pretty...
5
by: Charles Law | last post by:
I have posted this on a couple of Access and SQL Server newsgroups, but with no answer yet. There seems to be very little activity on the ones I have looked at, so I thought I would post here as...
1
by: cawoodm | last post by:
I need an identity XSLT that does not change the incoming XML document. The trick is that I need the entities to be preserved. My current transform converts   to the copyright symbol in the output...
2
by: Gibson | last post by:
Hi all, i transform xml A to xml B with an xslt. Is it possible to use that xslt to tranform xml B to xml A? Or exist tools to create a corresponding xslt B from the original xslt? Thanks for...
4
by: Tim Bücker | last post by:
Hello. I want to set my coordinate-system-origin (0/ 0) to the bottom-left corner. How is that done in C# the best way? Thanks a lot in advance! Greetings, Tim.
15
by: silverburgh.meryl | last post by:
Hi, I am trying to write a transform_until template. It is bascially doing what transform is doing except if UnaryOperation return NULL, it will break out from the loop. Here is the code, but...
4
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
6
by: Vijai Kalyan | last post by:
Hello, I am trying to use std::transform to take in a collection of strings, transform them and insert the result into an output container. So, I wrote something like this: std::wstring...
10
by: cjard | last post by:
I have a client and server that enjoy the following simple dialogue: Client connects Client sends request Server sends response Client disconnects This is the way it must be. The response...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.