std::transform (3)  Linux Manuals
std::transform: std::transform
NAME
std::transform  std::transform
Synopsis
Defined in header <algorithm>
template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, (until C++20)
UnaryOperation unary_op );
template< class InputIt, class OutputIt, class UnaryOperation >
constexpr OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, (since C++20)
UnaryOperation unary_op );
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryOperation >
ForwardIt2 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, (2) (since C++17)
ForwardIt2 d_first, UnaryOperation unary_op ); (1)
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, (until C++20)
OutputIt d_first, BinaryOperation binary_op );
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
constexpr OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, (3) (since C++20)
OutputIt d_first, BinaryOperation binary_op );
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3, class BinaryOperation >
ForwardIt3 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, (4) (since C++17)
ForwardIt2 first2, ForwardIt3 d_first, BinaryOperation binary_op );
std::transform applies the given function to a range and stores the result in another range, beginning at d_first.
1) The unary operation unary_op is applied to the range defined by [first1, last1).
3) 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.
2,4) Same as (1,3), but executed according to policy. This overload only participates in overload resolution if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true
unary_op and binary_op must not have side effects. (until C++11)
unary_op and binary_op must not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved. (since C++11)
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
policy  the execution policy to use. See execution_policy for details.
unary_op  Ret fun(const Type &a);
binary_op  Ret fun(const Type1 &a, const Type2 &b);
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
Output iterator to the element past the last element transformed.
Complexity
12) Exactly std::distance(first1, last1) applications of unary_op
34) Exactly std::distance(first1, last1) applications of binary_op
Exceptions
The overloads with a template parameter named ExecutionPolicy report errors as follows:
* If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard_policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementationdefined.
* If the algorithm fails to allocate memory, std::bad_alloc is thrown.
Possible implementation
First version
Second version
Notes
std::transform does not guarantee inorder application of unary_op or binary_op. To apply a function to a sequence inorder or to apply a function that modifies the elements of a sequence, use std::for_each
Example
The following code uses transform to convert a string in place to uppercase using the toupper function and then transforms each char to its ordinal value:
// Run this code
Output:
See also
for_each (function template)