std::exclusive_scan
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<numeric>
定义
|
||
(1) | ||
template<
class
InputIt, class
OutputIt, class
T >
OutputIt exclusive_scan(
InputIt first, InputIt
last, |
(C++17
起) (C++20 前) |
|
template<
class
InputIt, class
OutputIt, class
T >
constexpr
OutputIt
exclusive_scan(
InputIt first, InputIt
last, |
(C++20 起) | |
template<
class
ExecutionPolicy, class
ForwardIt1, class
ForwardIt2, class
T >
ForwardIt2
exclusive_scan(
ExecutionPolicy&&
policy, ForwardIt1
first, ForwardIt1
last, |
(2) | (C++17 起) |
(3) | ||
template<
class
InputIt, class
OutputIt,
class
T, class
BinaryOperation > |
(C++17
起) (C++20 前) |
|
template<
class
InputIt, class
OutputIt,
class
T, class
BinaryOperation > |
(C++20 起) | |
template<
class
ExecutionPolicy, class
ForwardIt1, class
ForwardIt2,
class
T, class
BinaryOperation > |
(4) | (C++17 起) |
用 binary_op
(或对于重载 (1-2) 是 std::plus<>() )计算范围
[first, last)
上排除性前缀和,以 init
为初始值,并写入结果到从
d_first
开始的范围。“排除性”表示第 i 个输入元素不包含于第 i 个和。
正式而言,用对于 [first, first +
(i - d_first)) 中的每个 j
在
binary_op
上的广义非交换和 init, *j...
的值,通过 [d_first, d_first +
(last - first)) 中的每个迭代器赋值。
其中广义非交换和 GNSUM(op, a
1, ..., a
N) 定义如下:
- 若 N=1 ,则为
a
1 - 若 N >
1 ,则为 op(GNSUM(op, a
1, ..., a
K), GNSUM(op, a
M, ..., a
N)) ,对于任何 1 < K+1 = M ≤ N 中的 K
换言之,和运算可能以任意顺序进行,而且若 binary_op
非结合,则行为是非确定的。
policy
执行。这些重载只有在
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
binary_op
不应非法化范围 [first, last) 或 [d_first, d_first +
(last - first)) 中的迭代器(含尾迭代器)或子范围,亦不应修改其中的元素。否则行为未定义。
参数
first, last | - | 要求和的元素范围 |
d_first | - | 目标范围的起始;可以等于 first
|
policy | - | 所用的执行策略。细节见执行策略。 |
init | - | 初始值 |
binary_op | - | 将应用到解引用输入迭代器的结果、其他 binary_op 的结果和
init 的二元函数对象
(FunctionObject)
。
|
类型要求 | ||
-InputIt
必须符合老式输入迭代器
(LegacyInputIterator)
的要求。
|
||
-
OutputIt 必须符合老式输出迭代器
(LegacyOutputIterator)
的要求。
|
||
-
ForwardIt1, ForwardIt2 必须符合老式向前迭代器
(LegacyForwardIterator)
的要求。
|
||
-T
必须符合可移动构造
(MoveConstructible)
的要求。且 binary_op(init, *first) 、
binary_op(init, init) 及
binary_op(*first, *first) 必须能转换到
T
|
返回值
指向最后写入元素的后一元素的迭代器。
复杂度
O(last - first) 次应用二元运算
异常
拥有名为 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 个输入 (函数模板) |