std::transform
|   在标头  <algorithm> 定义
  | 
||
|   template< class InputIt, class OutputIt, class UnaryOp > OutputIt transform( InputIt first1, InputIt last1,  | 
(1) |  (C++20 起为 constexpr)  | 
|   template< class ExecutionPolicy,           class ForwardIt1, class ForwardIt2, class UnaryOp >  | 
(2) | (C++17 起) | 
|   template< class InputIt1, class InputIt2,           class OutputIt, class BinaryOp >  | 
(3) |  (C++20 起为 constexpr)  | 
|   template< class ExecutionPolicy,           class ForwardIt1, class ForwardIt2,  | 
(4) | (C++17 起) | 
std::transform 应用给定的函数到某个/些输入范围中的元素,并将结果存储到从 d_first 开始的输出范围。
[first1, last1) 中的元素。-  
[first1,last1]。 - 包含 std::distance(first1, last1) + 1 个元素的从 d_first 开始的范围。
 
[first1, last1),而另一个范围包含 std::distance(first1, last1) 个元素并从 first2 开始。-  
[first1,last1]。 - 包含 std::distance(first1, last1) + 1 个元素的从 first2 开始的范围。
 - 包含 std::distance(first1, last1) + 1 个元素的从 d_first 开始的范围。
 
| 
 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>  | 
(C++20 前) | 
| 
 std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>  | 
(C++20 起) | 
目录 | 
[编辑] 参数
| first1, last1 | - | 要变换的第一元素范围 | 
| first2 | - | 要变换的第二元素范围的起始 | 
| d_first | - | 目标范围的起始,可以等于 first1 或 first2 | 
| policy | - | 所用的执行策略。细节见执行策略。 | 
| unary_op | - |  将要应用的一元算符函数。 函数签名应等价于如下者: Ret fun(const Type &a); 签名不必有 const & 。  | 
| binary_op | - |  被使用的二元函数对象。 该函数的签名应当等价于: Ret fun(const Type1 &a, const Type2 &b); 签名中并不需要有 const &。  | 
| 类型要求 | ||
 -InputIt, InputIt1, InputIt2 必须满足老式输入迭代器 (LegacyInputIterator) 。
 | ||
 -OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。
 | ||
 -ForwardIt1, ForwardIt2, ForwardIt3 必须满足老式向前迭代器 (LegacyForwardIterator) 。
 | ||
[编辑] 返回值
指向最后一个变换的元素的输出迭代器。
[编辑] 复杂度
给定 N 为 std::distance(first1, last1):
[编辑] 异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
-  如果作为算法一部分调用的函数的执行抛出异常,且 
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
 
[编辑] 可能的实现
| transform (1) | 
|---|
template<class InputIt, class OutputIt, class UnaryOp> OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOp unary_op) { while (first1 != last1) *d_first++ = unary_op(*first1++); return d_first; }  | 
| transform (3) | 
template<class InputIt1, class InputIt2, class OutputIt, class BinaryOp> OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOp binary_op) { while (first1 != last1) *d_first++ = binary_op(*first1++, *first2++); return d_first; }  | 
[编辑] 注解
std::transform 不保证按顺序应用 unary_op 或 binary_op。为按顺序应用函数到数列,或应用修改序列元素的函数,应使用 std::for_each。
[编辑] 示例
#include <algorithm> #include <cctype> #include <iomanip> #include <iostream> #include <string> #include <utility> #include <vector> void print_ordinals(const std::vector<std::size_t>& ordinals) { std::cout << "序数:"; for (std::size_t 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) { // 原位变换字符串为大写 std::transform(hello.cbegin(), hello.cend(), hello.begin(), to_uppercase); std::cout << "hello = " << std::quoted(hello) << '\n'; // for_each 版本(见上文注解部分) 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) { // 变换数值为两倍的值 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)); }
输出:
hello = "HELLO" world = "WORLD" 序数: 72 69 76 76 79 序数:144 138 152 152 158
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 242 | C++98 | unary_op 和 binary_op 不能有任何副作用 | 它们不能修改涉及到的范围 | 
[编辑] 参阅
|    应用函数到范围中的元素   (函数模板)  | |
|    (C++20)  | 
  将一个函数应用于某一范围的各个元素   (niebloid)  |