stl:algorithm:transform

C++ Reference

transform

Syntax:

    #include <algorithm>
    iterator transform( iterator start, iterator end, iterator result, UnaryFunction f );
    iterator transform( iterator start1, iterator end1, iterator start2, 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