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]。 - 从 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 。 |
(C++20 前) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 为 true 。 |
(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) { // 原地转换字符串为大写 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" ordinals: 72 69 76 76 79 ordinals: 144 138 152 152 158
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 242 | C++98 | unary_op 和 binary_op 不能具有副作用 | 它们不能修改所涉及的区间 |
参见
|
对
范围
内的元素应用一元
函数对象
(函数模板) |
|
|
(C++20)
|
对元素范围应用函数
(算法函数对象) |