std::transform(3)
π std::transform π
okay bestie, this is the C++ Standard Libary manual entry (their spelling, not mine π) for the one and only std::transform. Itβs literally the βtake your elements and glow them up through a functionβ algorithm, and yes, weβre gonna keep every single detail. no cap.
π NAME π
std::transform β std::transform
β¨ Synopsis β¨
Defined in header <algorithm>.
template< class InputIt, class OutputIt, class UnaryOp >
OutputIt transform( InputIt first1, InputIt last1,
OutputIt d_first, UnaryOp unary_op );
- (1) β
constexpr since C++20.
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class UnaryOp >
ForwardIt2 transform( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 d_first, UnaryOp unary_op );
- (2) β
since C++17.
template< class InputIt1, class InputIt2,
class OutputIt, class BinaryOp >
OutputIt transform( InputIt1 first1, InputIt1 last1,
InputIt2 first2,
OutputIt d_first, BinaryOp binary_op );
- (3) β
constexpr since C++20.
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2,
class ForwardIt3, class BinaryOp >
ForwardIt3 transform( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2,
ForwardIt3 d_first, BinaryOp binary_op );
- (4) β
since C++17.
std::transform applies the given function to the elements of the given input range(s), and stores the result in an output range starting from d_first. cute, efficient, iconic.
1) The unary operation unary_op is applied to the elements of [first1, last1).
If unary_op invalidates an iterator or modifies an element in any of the following ranges, the behavior is undefined:
[first1, last1).- The range of
std::distance(first1, last1) + 1elements starting fromd_first.
3) The binary operation binary_op is applied to pairs of elements from two ranges: [first1, last1) and another range of std::distance(first1, last1) elements starting from first2.
If binary_op invalidates an iterator or modifies an element in any of the following ranges, the behavior is undefined:
[first1, last1).- The range of
std::distance(first1, last1) + 1elements starting fromfirst2. - The range of
std::distance(first1, last1) + 1elements starting fromd_first.
2,4) Same as (1,3), but executed according to policy.
These overloads participate in overload resolution only if
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>is true. (until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>is true. (since C++20)
πΈ 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 tofirst1orfirst2.policyβ the execution policy to use. See execution policy for details.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,InputIt1,InputIt2must meet the requirements ofLegacyInputIterator.OutputItmust meet the requirements ofLegacyOutputIterator.ForwardIt1,ForwardIt2,ForwardIt3must meet the requirements ofLegacyForwardIterator.
π Return value π
Output iterator to the element that follows the last element transformed. periodt.
β Complexity β
Given \(\scriptsize N\)N as std::distance(first1, last1):
- 1,2) Exactly
\(\scriptsize N\)Napplications ofunary_op. - 3,4) Exactly
\(\scriptsize N\)Napplications ofbinary_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
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π Possible implementation π
transform (1)
template<class InputIt, class OutputIt, class UnaryOp>
constexpr //< since C++20
OutputIt transform(InputIt first1, InputIt last1,
OutputIt d_first, UnaryOp unary_op)
{
for (; first1 != last1; ++d_first, ++first1)
*d_first = unary_op(*first1);
return d_first;
}
transform (3)
template<class InputIt1, class InputIt2,
class OutputIt, class BinaryOp>
constexpr //< since C++20
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
OutputIt d_first, BinaryOp binary_op)
{
for (; first1 != last1; ++d_first, ++first1, ++first2)
*d_first = binary_op(*first1, *first2);
return d_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. iykyk.
π Example π
// Run this code
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
void print_ordinals(const std::vector<unsigned>& ordinals)
{
std::cout << "ordinals: ";
for (unsigned ord : ordinals)
std::cout << std::setw(3) << ord << ' ';
std::cout << '\n';
}
char to_uppercase(unsigned char c)
{
return std::toupper(c);
}
void to_uppercase_inplace(char& c)
{
c = to_uppercase(c);
}
void unary_transform_example(std::string& hello, std::string world)
{
// Transform string to uppercase in-place
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';
}
void binary_transform_example(std::vector<unsigned> ordinals)
{
// Transform numbers to doubled values
print_ordinals(ordinals);
std::transform(ordinals.cbegin(), ordinals.cend(), ordinals.cbegin(),
ordinals.begin(), std::plus<>{});
print_ordinals(ordinals);
}
int main()
{
std::string hello("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.
- LWG 242 β applied to C++98. Behavior as published:
unary_opandbinary_opcould not have side effects. Correct behavior: they cannot modify the ranges involved.
π See also π
for_eachβ applies a function to a range of elements (function template).ranges::transformβ applies a function to a range of elements (C++20) (niebloid).
source/version/date check, bestie: http://cppreference.com Β· 2024.06.10 Β· page std::transform(3) β mwah, slay π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms