transform [C++ Reference]
Keywords: stl,algorithm,transform
Date: 2009-10-09T01:11:55-0700

[[transform]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## transform

Syntax:

        #include <algorithm>
        output_iterator transform( input_iterator start, input_iterator end, output_iterator result, UnaryFunction f );
        output_iterator transform( input_iterator start1, input_iterator end1, input_iterator2 start2, output_iterator result, BinaryFunction f );

The transform() algorithm applies the function f to some range of elements, storing the result of each application of the function in result.

The first version of the function applies f to each element in [start,end) and assigns the first output of the function to result, the second output to (result+1), etc.

The second version of the transform() works in a similar manner, except that it is given two ranges of elements and calls a binary function on a pair of elements.

For example, the following code uses transform() to convert a string to uppercase using the standard C library toupper function:

        string s("hello");
        transform(s.begin(), s.end(), s.begin(), toupper);
        cout << s << endl;

The above code displays the following output:

        HELLO

Related Topics: copy, fill, generate

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •