std::exclusive_scan
定义于头文件 <numeric> |
||
template< class InputIt, class OutputIt, class T > OutputIt exclusive_scan( InputIt first, InputIt last, |
(1) | (自 C++17 起) (constexpr 自 C++20 起) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T > |
(2) | (自 C++17 起) |
template< class InputIt, class OutputIt, class T, class BinaryOp > |
(3) | (自 C++17 起) (constexpr 自 C++20 起) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, |
(4) | (自 C++17 起) |
[
0,
std::distance(first, last))
中,按顺序执行以下操作- 创建一个序列,该序列由 init 后跟
[
first,
iter)
中的元素按顺序组成,其中 iter 是 first 的下一个第 i 个迭代器。 - 计算序列在 op 上的广义非交换和。
- 将结果赋值给 *dest,其中 dest 是 d_first 的下一个第 i 个迭代器。
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 作为实际的二元运算
- 如果 binary_op 不满足结合律(例如浮点加法),则结果是不确定的。
- 如果以下任何值无法转换为
T
,则程序是非良构的
- binary_op(init, *first)
- binary_op(init, init)
- binary_op(*first, *first)
- 如果满足以下任何条件,则行为是未定义的
-
T
不是 可移动构造 (MoveConstructible)。 - binary_op 修改了
[
first,
last)
的任何元素。 - binary_op 使
[
first,
last]
的任何迭代器或子范围无效。
-
目录 |
[编辑] 参数
first, last | - | 定义要累加的元素的范围的迭代器对 |
d_first | - | 目标范围的起始;可能等于 first |
policy | - | 要使用的执行策略 |
init | - | 初始值 |
op | - | 二元函数对象 (FunctionObject),将被应用于解引用输入迭代器的结果、其他 op 的结果和 init |
类型要求 | ||
-InputIt 必须满足 遗留输入迭代器 (LegacyInputIterator) 的要求。 | ||
-OutputIt 必须满足 遗留输出迭代器 (LegacyOutputIterator) 的要求。 | ||
-ForwardIt1, ForwardIt2 必须满足 遗留前向迭代器 (LegacyForwardIterator) 的要求。 |
[编辑] 返回值
指向写入的最后一个元素之后的位置的迭代器。
[编辑] 复杂度
给定 N 为 std::distance(first, last)
[编辑] 异常
具有名为 ExecutionPolicy
的模板参数的重载按如下方式报告错误
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 示例
#include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector data{3, 1, 4, 1, 5, 9, 2, 6}; std::cout << "Exclusive sum: "; std::exclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), 0); std::cout << "\nInclusive sum: "; std::inclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n\nExclusive product: "; std::exclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), 1, std::multiplies<>{}); std::cout << "\nInclusive product: "; std::inclusive_scan(data.begin(), data.end(), std::ostream_iterator<int>(std::cout, " "), std::multiplies<>{}); }
输出
Exclusive sum: 0 3 4 8 9 14 23 25 Inclusive sum: 3 4 8 9 14 23 25 31 Exclusive product: 1 3 3 12 12 60 540 1080 Inclusive product: 3 3 12 12 60 540 1080 6480
[编辑] 参见
计算范围中相邻元素之间的差值 (函数模板) | |
对元素范围求和或折叠 (函数模板) | |
计算元素范围的部分和 (函数模板) | |
(C++17) |
应用可调用对象,然后计算独占扫描 (函数模板) |
(C++17) |
类似于 std::partial_sum,在第 ith 个和中包含第 ith 个输入元素 (函数模板) |