std::transform
定义于头文件 <algorithm> |
||
template< class InputIt, class OutputIt, class UnaryOp > OutputIt transform( InputIt first1, InputIt last1, |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryOp > |
(2) | (since C++17) |
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOp > |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(4) | (since C++17) |
std::transform
将给定的函数应用于给定输入范围的元素,并将结果存储在从 d_first 开始的输出范围中。
[
first1,
last1)
的元素。-
[
first1,
last1]
. - 从 d_first 开始的 std::distance(first1, last1) + 1 个元素的范围。
[
first1,
last1)
和另一个从 first2 开始的 std::distance(first1, last1) 个元素的范围。-
[
first1,
last1]
. - 从 first2 开始的 std::distance(first1, last1) + 1 个元素的范围。
- 从 d_first 开始的 std::distance(first1, last1) + 1 个元素的范围。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(since C++20) |
内容 |
[编辑] 参数
first1, last1 | - | 定义要转换元素的源范围的迭代器对 |
first2 | - | 要转换的第二个元素范围的开始,仅限 (3,4) |
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> 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; } |
[编辑] 注意
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<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)); }
输出
hello = "HELLO" world = "WORLD" ordinals: 72 69 76 76 79 ordinals: 144 138 152 152 158
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 242 | C++98 | unary_op 和 binary_op 不能有副作用 | 它们不能修改所涉及的范围 |
[编辑] 参见
将一元函数对象应用于来自范围的元素 (函数模板) | |
(C++20) |
将函数应用于元素范围 (算法函数对象) |