std::transform - cppreference.com

Defined in header

<algorithm>

template<classInputIt,classOutputIt,classUnaryOp>OutputIttransform(InputItfirst1,InputItlast1,OutputItd_first,UnaryOpunary_op); (1)(constexpr since C++20)template<classInputIt1,classInputIt2,classOutputIt,classBinaryOp>OutputIttransform(InputIt1first1,InputIt1last1,InputIt2first2,OutputItd_first,BinaryOpbinary_op); (2)(constexpr since C++20)template<classExecutionPolicy,classForwardIt1,classForwardIt2,classUnaryOp>ForwardIt2transform(ExecutionPolicy&&policy,ForwardIt1first1,ForwardIt1last1,ForwardIt2d_first,UnaryOpunary_op); (3) (since C++17)template<classExecutionPolicy,classForwardIt1,classForwardIt2,classForwardIt3,classBinaryOp>ForwardIt3transform(ExecutionPolicy&&policy,ForwardIt1first1,ForwardIt1last1,ForwardIt2first2,ForwardIt3d_first,BinaryOpbinary_op); (4) (since C++17)Given count as std::distance(first1,last1). Applies the given function to the elements of the given source range(s), and stores the result in the destination range [d_first, std::next(d_first,count)).

1) There is only one source range [first1, last1), and the unary operation unary_op is applied to each of its elements.

If unary_op invalidates an iterator or modifies an element in any of the following ranges, the behavior is undefined:

[first1, last1]

[d_first, std::next(d_first,count)]

2) There are two source ranges [first1, last1) and [first2, std::next(first2,count)), and the binary operation binary_op is applied to each pair of their corresponding elements.

If binary_op invalidates an iterator or modifies an element in any of the following ranges, the behavior is undefined:

[first1, last1]

[first2, std::next(first2,count)]

[d_first, std::next(d_first,count)]

3,4) Same as (1,2), but executed according to policy.

These overloads participate in overload resolution only if the value of the following expression is true:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(since C++20)Parameters

first1, last1 - the pair of iterators defining the first source

range

first2 - the beginning of the second source range 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:

Retfun(constType&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:

Retfun(constType1&a,constType2&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. ​

policy - the

execution policy

to use Type requirements -InputIt, InputIt1, InputIt2 must meet the requirements of

LegacyInputIterator

. -OutputIt must meet the requirements of

LegacyOutputIterator

. -ForwardIt1, ForwardIt2, ForwardIt3 must meet the requirements of

LegacyForwardIterator

. Return value

The past-the-end iterator of the destination range.

Complexity

Given N as std::distance(first1,last1):

1,3) Exactly N applications of unary_op.

2,4) Exactly N applications of binary_op.

Exceptions

3,4) During the execution process:

If the temporary memory resources required for parallelization are not available,

std::bad_alloc

is thrown.

If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for

standard policies

,

std::terminate

is invoked).

Possible implementation

transform (1)

template<classInputIt,classOutputIt,classUnaryOp>constexpr//< since C++20OutputIttransform(InputItfirst1,InputItlast1,OutputItd_first,UnaryOpunary_op){for(;first1!=last1;++d_first,++first1)*d_first=unary_op(*first1);returnd_first;}

transform (2)

template<classInputIt1,classInputIt2,classOutputIt,classBinaryOp>constexpr//< since C++20OutputIttransform(InputIt1first1,InputIt1last1,InputIt2first2,OutputItd_first,BinaryOpbinary_op){for(;first1!=last1;++d_first,++first1,++first2)*d_first=binary_op(*first1,*first2);returnd_first;}Notes

std::transform does not guarantee in-order application of unary_op or binary_op. To apply a function to a sequence in-order or to apply a function that modifies the elements of a sequence, use

std::for_each

.

Example

Run this code

#include<algorithm>#include<cctype>#include<iomanip>#include<iostream>#include<string>#include<utility>#include<vector>voidprint_ordinals(conststd::vector<unsigned>&ordinals){std::cout<<"ordinals: ";for(unsignedord:ordinals)std::cout<<std::setw(3)<<ord<<' ';std::cout<<'\n';}charto_uppercase(unsignedcharc){returnstd::toupper(c);}voidto_uppercase_inplace(char&c){c=to_uppercase(c);}// Transform string to uppercase in-placevoidunary_transform_example(std::string&hello,std::stringworld){std::transform(hello.cbegin(),hello.cend(),hello.begin(),to_uppercase);std::cout<<"hello = "<<std::quoted(hello)<<'\n';// for_each version (see Notes above)std::for_each(world.begin(),world.end(),to_uppercase_inplace);std::cout<<"world = "<<std::quoted(world)<<'\n';}// Transform numbers to doubled valuesvoidbinary_transform_example(std::vector<unsigned>ordinals){print_ordinals(ordinals);std::transform(ordinals.cbegin(),ordinals.cend(),ordinals.cbegin(),ordinals.begin(),std::plus<>{});print_ordinals(ordinals);}intmain(){std::stringhello("hello");unary_transform_example(hello,"world");std::vector<unsigned>ordinals;std::copy(hello.cbegin(),hello.cend(),std::back_inserter(ordinals));binary_transform_example(std::move(ordinals));}Output:

hello = "HELLO" world = "WORLD" ordinals: 72 69 76 76 79 ordinals: 144 138 152 152 158 Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

LWG 242

C++98 unary_op and binary_op could not have side effects they cannot modify the ranges involved See also