std::exclusive_scan
来自 zh.cppreference.net
| 定义于头文件 <numeric>
|
||
template< class InputIt, class OutputIt, class T >
OutputIt exclusive_scan( InputIt first, InputIt last,
OutputIt d_first, T init );
|
(1) | (C++17 起) (C++20 起 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class T >
ForwardIt2 exclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, T init );
|
(2) | (C++17 起) |
template< class InputIt, class OutputIt,
class T, class BinaryOp >
OutputIt exclusive_scan( InputIt first, InputIt last,
OutputIt d_first, T init, BinaryOp op );
|
(3) | (C++17 起) (C++20 起 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2,
class T, class BinaryOp >
ForwardIt2 exclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 d_first, T init, BinaryOp op );
|
(4) | (C++17 起) |
1) 等价于
exclusive_scan(first, last, d_first, init, std::plus<>()。3) 使用
op 计算排他性前缀和。对于每个整数
i 在 [0, std::distance(first, last)) 中,按顺序执行以下操作:
- 创建一个序列,该序列由
init后跟按顺序的[first,iter)的元素组成,其中iter是i-th 的下一个迭代器first。 - 计算序列上关于
op的广义非交换和。 - 将结果赋值给
*dest,其中dest是i-th 的下一个迭代器d_first。
2,4) 与 (1,3) 相同,但根据
policy 执行。这些重载仅在以下表达式的值为
true 时参与重载决议:
|
|
(C++20 前) |
|
|
(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 | - | 二元 函数对象 ,将应用于解引用输入迭代器的结果、其他 op 的结果以及 init |
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求
|
||
-
OutputIt
必须满足
LegacyOutputIterator
的要求
|
||
-
ForwardIt1, ForwardIt2
必须满足
LegacyForwardIterator
的要求
|
||
返回值
指向已写入的最后一个元素之后位置的迭代器。
复杂度
给定 N 为 std:: distance ( first, last ) :
3,4)
O(N)
次
op
的应用。
异常
带有名为
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
,在第
i
个和中包含第
i
个输入元素
(函数模板) |