471,071 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

transform

Hello

Is it correct to use

string s = "lower case";
transform(s.begin(),s.end(),s.begin(),toupper);

to make s uppercase ??

this was working before but it doesnt work with gcc 3

thanks

Aug 8 '05 #1
6 2967
Babis Haldas wrote:
Is it correct to use

string s = "lower case";
transform(s.begin(),s.end(),s.begin(),toupper);

to make s uppercase ??
Seems fine. I checked with a couple other compilers, they like it.
Did you provide all the necessary headers?
this was working before but it doesnt work with gcc 3


Read FAQ 5.8, then.

V
Aug 8 '05 #2
>string s = "lower case";
transform(s.begin(),s.end(),s.begin(),toupper);
to make s uppercase ??

it is absolutely correct.

plz see http://www.josuttis.com/libbook/string/iter1.cpp.html
for the same...

thanks
rt

Aug 8 '05 #3
What is wrong with for_each? Josuttis has it in his table of modifying
algorithms.

Fraser.
Aug 8 '05 #4
Fraser Ross wrote:
What is wrong with for_each? Josuttis has it in his table of modifying
algorithms.


'for_each' is designed for calling essentially a 'void' function. That
function can modify its argument, but the return value of that function
is not used by 'for_each'. 'transform', OTOH, takes the functor and
uses the return value of each invocation to assign to the object from
the other sequence.

Just try writing the uppercasing using for_each, you will have to create
a modifying functor for that.

V
Aug 8 '05 #5

Babis Haldas wrote:
Hello

Is it correct to use

string s = "lower case";
transform(s.begin(),s.end(),s.begin(),toupper);

to make s uppercase ??

this was working before but it doesnt work with gcc 3

thanks


hello, what's the error? we're not psychic. well, maybe. i would guess
that you have multiple overloads of toupper in scope and the compiler
doesn't know which one to choose.

static_cast<int(*)(int)>(toupper) or std::ptr_fun<int, int>(toupper)

Aug 8 '05 #6

"Victor Bazarov"
Fraser Ross wrote:
What is wrong with for_each? Josuttis has it in his table of modifying
algorithms.


'for_each' is designed for calling essentially a 'void' function. That
function can modify its argument, but the return value of that function
is not used by 'for_each'. 'transform', OTOH, takes the functor and
uses the return value of each invocation to assign to the object from
the other sequence.

Just try writing the uppercasing using for_each, you will have to create
a modifying functor for that.

The behaviour required is not present with the function toupper. I see
for_each as definitely being the right algorithm. I would rather make a
function or functor work than use the wrong algorithm.

A missing algorithm is one that modifies a range using a second range and a
binary operation:

template <typename ForwardIterator_tp, typename InputIterator_tp, typename
BinaryOperation_tp>
BinaryOperation_tp transform_each(ForwardIterator_tp first1,
InputIterator_tp const last1, InputIterator_tp first2,
BinaryOperation_tp binaryOp) {
while (first1 != last1) {
binaryOp(*first1, *first2);
++first1, ++first2;
}
return binaryOp;
}
Fraser.
Aug 8 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Xiaolei Li | last post: by
2 posts views Thread by John Lehmann | last post: by
9 posts views Thread by Patrick Guio | last post: by
6 posts views Thread by Stephen Cook | last post: by
4 posts views Thread by Dean Card | last post: by
6 posts views Thread by Vijai Kalyan | last post: by
reply views Thread by leo001 | last post: by

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.