std::transform_inclusive_scan
定义于头文件 <numeric> |
||
template< class InputIt, class OutputIt, class BinaryOp, class UnaryOp > |
(1) | (C++17 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(2) | (C++17 起) |
template< class InputIt, class OutputIt, class BinaryOp, class UnaryOp, class T > |
(3) | (C++17 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(4) | (C++17 起) |
[
0,
std::distance(first, last))
中的每个整数 i,按顺序执行以下操作:- 创建一个序列,该序列由
[
first,
iter]
的元素经 unary_op 按顺序转换而成,其中 iter 是 first 的第 i 个迭代器。 - 计算序列在 binary_op 上的广义非交换和。
- 将结果赋值给 *dest,其中 dest 是 d_first 的第 i 个迭代器。
[
first,
iter]
元素组成。
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 起) |
序列在二元操作 binary_op 上的广义非交换和定义如下:
- 如果序列只有一个元素,则和就是该元素的值。
- 否则,按顺序执行以下操作:
- 从序列中选择任意两个相邻元素 elem1 和 elem2。
- 计算 binary_op(elem1, elem2) 并用结果替换序列中的这两个元素。
- 重复步骤 1 和 2,直到序列中只剩下一个元素。
如果 binary_op 不满足结合律(例如浮点加法),则结果是不确定的。
对于重载 (1,2),如果 binary_op(unary_op(*first), unary_op(*first)) 不能转换为 decltype(first) 的值类型,则程序格式错误。
对于重载 (3,4),如果以下任何值不能转换为 T
,则程序格式错误:
- binary_op(init, init)
- binary_op(init, unary_op(*first))
- binary_op(unary_op(*first), unary_op(*first))
如果满足以下任何条件,则行为是未定义的:
- 对于重载 (1,2),decltype(first) 的值类型不是可移动构造的 (MoveConstructible)。
- 对于重载 (3,4),
T
不是可移动构造的 (MoveConstructible)。 - unary_op 或 binary_op 修改
[
first,
last)
的任何元素。 - unary_op 或 binary_op 使
[
first,
last]
的任何迭代器或子范围失效。
目录 |
[编辑] 参数
first, last | - | 定义要求和元素的范围的迭代器对 |
d_first | - | 目标范围的起始;可以等于 first |
policy | - | 要使用的 执行策略 |
init | - | 初始值 |
unary_op | - | 将应用于输入范围的每个元素的单目函数对象。返回类型必须是 binary_op 可接受的输入。 |
binary_op | - | 将应用于 unary_op 的结果、其他 binary_op 的结果以及(如果提供)init 的二目函数对象。 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-OutputIt 必须满足 LegacyOutputIterator 的要求。 | ||
-ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。 |
[编辑] 返回值
指向最后一个写入元素之后的元素的迭代器。
[编辑] 复杂度
给定 N 为 std::distance(first, last)
[编辑] 异常
带有模板参数 ExecutionPolicy
的重载按如下方式报告错误
- 如果作为算法一部分调用的函数执行时抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法未能分配内存,则抛出 std::bad_alloc。
[编辑] 注意
unary_op 从未应用于 init。
参数 init 出现在最后,这与 std::transform_exclusive_scan 不同,因为对于此函数它是可选的。
[编辑] 示例
#include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector data{3, 1, 4, 1, 5, 9, 2, 6}; auto times_10 = [](int x) { return x * 10; }; std::cout << "10 times exclusive sum: "; std::transform_exclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), 0, std::plus<int>{}, times_10); std::cout << "\n10 times inclusive sum: "; std::transform_inclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), std::plus<int>{}, times_10); std::cout << '\n'; }
输出
10 times exclusive sum: 0 30 40 80 90 140 230 250 10 times inclusive sum: 30 40 80 90 140 230 250 310
[编辑] 参阅
计算一个范围元素的部分和 (函数模板) | |
对一个范围的元素应用函数,并将结果存储在目标范围中 (函数模板) | |
(C++17) |
类似于 std::partial_sum,将第 i 个输入元素包含在第 i 个和中。 (函数模板) |
(C++17) |
应用一个可调用对象,然后计算排除扫描 (函数模板) |