Namespaces
Variants
Views
Actions

std::transform

From cppreference.com
< cpp‎ | algorithm
Revision as of 16:41, 27 March 2013 by SuperBoi45 (Talk | contribs)

 
 
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class InputIt, class OutputIt, class UnaryOperation >

OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first,

                    UnaryOperation unary_op );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >

OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2,

                    OutputIt d_first, BinaryOperation binary_op );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

std::transform 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 the binary operation binary_op is applied to pairs of elements from two ranges: one defined by [first1, last1) and the 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, may be equal to first1 or first2
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 InputIt can be dereferenced and then implicitly converted to Type. The type Ret must be such that an object of type OutputIt 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 InputIt1 and InputIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. ​

Type requirements
-
InputIt must meet the requirements of InputIterator.
-
InputIt1 must meet the requirements of InputIterator.
-
InputIt2 must meet the requirements of InputIterator.
-
OutputIt must meet the requirements of OutputIterator.

Return value

output iterator to the element past the last element transformed.

Complexity

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

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

Requirements

unary_op and binary_op have no side effects. (until C++11)

unary_op and binary_op do not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved. (since C++11)

The intent of these requirements is to allow parallel or out-of-order implementations of std::transform. To apply a function to a sequence in-order, use std::for_each.

Possible implementation

First version
template<class InputIt, class OutputIt, class UnaryOperation>
OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, 
                   UnaryOperation unary_op)
{
    while (first1 != last1) {
        *d_first++ = unary_op(*first1++);
    }
    return d_first;
}
Second version
template<class InputIt1, class InputIt2, 
         class OutputIt, class BinaryOperation>
OutputIt transform(InputIt first1, InputIt last1, InputIt first2, 
                   OutputIt 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 <functional>
#include <iostream>
 
int main()
{
    std::string s("hello");
    std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper));
    std::cout << s;
 }

Output:

HELLO

See also

Template:cpp/algorithm/dcl list for each