Re: Reverse a String
On Oct 6, 4:33 pm, arnuld <geek.arn...@gmail.com> wrote:
On Fri, 05 Oct 2007 21:32:28 -0700, Kai-Uwe Bux wrote:
That function looks weird: it takes in_str by reference (indicating that
it might want to modify it) Then, however, it never does that. Instead,
it returns the reverted string. Either, you should do:
...[SNIP]....
Yes, but it does not revert anything. :-(
ok, think I have understood it now. for_each() should be used with a
function that actually reverses a string:
void revert_string( std::string& in_str )
{
return std::reverse( in_str.begin(), in_str.end() );
}
whereas, transform(), is good for using a function that returns a copy of
the input string.
RIGHT ?
No. Transform is used when you want to modify each element of
the sequence, independently of the other elements. Reverse is
used when you want to reverse the order of the elements, without
modifying any of them. Both have there uses, but neither is
what one would call frequent (particularly on strings---I've
never found a real use for either on a string).
All STL algorithms take ranges by pairs of iterators. If you want to
pass a container, you have to write a small wrapper around the
algorithm.
wrapper around the algorithm ?
Yes.
I did not understand that, did you mean like what we did here, by
creating a separate function ?
Exactly.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34