By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

transform

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
>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

P: n/a
What is wrong with for_each? Josuttis has it in his table of modifying
algorithms.

Fraser.
Aug 8 '05 #4

P: n/a
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

P: n/a

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

P: n/a

"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.