std::transform

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>

template< class InputIterator, class OutputIterator, class UnaryOperation >

OutputIterator transform( InputIterator first1, InputIterator1 last1,

                          OutputIterator d_first, UnaryOperation unary_op );
(1)
template< class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation >

OutputIterator transform( InputIterator1 first1, InputIterator1 last1,

                          InputIterator2 first2, OutputIterator d_first, BinaryOperation binary_op );
(2)

Applies the given function to a range and stores the result in another range, beginning at d_first. In the first version unary operation unary_op is applied to the range, defined by [first1, last1). In the second version binary operation binary_op is applied to pairs of elements from two ranges: one defined by [first1, last1) and other beginning at first2.

Contents

Parameters

first1, last1 - the first range of elements to transform
first2 - the beginning of the second range of elements to transform
d_first - the beginning of the destination range
unary_op - unary operation function object that will be applied.

The signature of the function should be equivalent to the following:

​Ret fun(const Type &a);

The signature does not need to have const &.
The type ​Type​ must be such that an object of type ​InputIterator​ can be dereferenced and then implicitly converted to ​Type​. The type ​Ret​ must be such that an object of type ​OutputIterator​ can be dereferenced and assigned a value of type ​Ret​. ​

binary_op - binary operation function object that will be applied.

The signature of the function should be equivalent to the following:

​Ret fun(const Type1 &a, const Type2 &b);

The signature does not need to have const &.
The types ​Type1​ and ​Type2​ must be such that objects of types ​InputIterator1​ and ​InputIterator2​ can be dereferenced and then implicitly converted to ​Type1​ and ​Type2​ respectively. The type ​Ret​ must be such that an object of type ​OutputIterator​ can be dereferenced and assigned a value of type ​Ret​. ​

Return value

output iterator to the element past the last element transformed.

Equivalent function

First version:
template<class InputIterator, class OutputIterator, class UnaryOperation>
OutputIterator copy(InputIterator first1, InputIterator last1,
                    OutputIterator d_first, UnaryOperation unary_op)
{
    while (first1 != last1) {
        *d_first++ = unary_op(*first1++);
    }
    return d_first;
}
Second version:
template<class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
OutputIterator copy(InputIterator first1, InputIterator last1,
                    InputIterator first2, OutputIterator d_first, BinaryOperation binary_op)
{
    while (first1 != last1) {
        *d_first++ = binary_op(*first1++, *first2++);
    }
    return d_first;
}

Example

The following code uses transform to convert a string to uppercase using the toupper function:

#include <string>
#include <cctype>
#include <algorithm>
#include <iostream>
 
int main()
{
    std::string s("hello");
    std::transform(s.begin(), s.end(), s.begin(), (int (*)(int))std::toupper);
    std::cout << s;
 
    return 0;
}

Output:

​HELLO​

Complexity

1) exactly ​std::distance(first1, last1) applications of unary_op

2) exactly ​std::distance(first1, last1) applications of binary_op

See also

for_each
applies a function to a range of elements
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages