std::transform
来自cppreference.com
<tbody>
</tbody>
| 在标头 <algorithm> 定义
|
||
template< class InputIt, class OutputIt, class UnaryOp > OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOp unary_op ); |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryOp > ForwardIt2 transform( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 d_first, UnaryOp unary_op ); |
(2) | (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) | (C++20 起为 constexpr) |
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) | (C++17 起) |
std::transform 应用给定的函数到某个/些输入范围中的元素,并将结果存储到从 d_first 开始的输出范围。
1) 应用一元函数
unary_op 到 [first1, last1) 中的元素。 如果
unary_op 使以下某个范围中的某个迭代器失效,或者修改了以下某个范围中的某个元素,那么行为未定义:
[first1,last1]。- 包含
std::distance(first1, last1) + 1个元素的从d_first开始的范围。
3) 应用二元函数
binary_op 到来自两个范围的元素对:一个范围是 [first1, last1),而另一个范围包含 std::distance(first1, last1) 个元素并从 first2 开始。 如果
binary_op 使以下某个范围中的某个迭代器失效,或者修改了以下某个范围中的某个元素,那么行为未定义:
[first1,last1]。- 包含
std::distance(first1, last1) + 1个元素的从first2开始的范围。 - 包含
std::distance(first1, last1) + 1个元素的从d_first开始的范围。
2,4) 同 (1,3),但按照
policy 执行。 这些重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first1, last1 | - | 要变换的第一源元素范围的迭代器对 |
| first2 | - | 要变换的第二元素范围的起始,仅限 (3,4) |
| d_first | - | 目标范围的起始,可以等于 first1 或 first2
|
| policy | - | 所用的执行策略 |
| unary_op | - | 将要应用的一元算符函数。 函数签名应等价于如下者:
签名不必有 |
| binary_op | - | 被使用的二元函数对象。 该函数的签名应当等价于:
签名中并不需要有 |
| 类型要求 | ||
-InputIt, InputIt1, InputIt2 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。
| ||
-ForwardIt1, ForwardIt2, ForwardIt3 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
返回值
指向最后一个变换的元素的输出迭代器。
复杂度
给定 N 为 std::distance(first1, last1):
1,2) 应用 N 次
unary_op。3,4) 应用 N 次
binary_op。异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| transform (1) |
|---|
template<class InputIt, class OutputIt, class UnaryOp>
constexpr //< 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 //< 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;
}
|
注解
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) |
应用函数到元素范围 (算法函数对象) |