std::inclusive_scan
在标头
<numeric>
定义
|
||
(1) | ||
template<
class
InputIt, class
OutputIt >
OutputIt inclusive_scan(
InputIt first, |
(C++17
起) (C++20 前) |
|
template<
class
InputIt, class
OutputIt >
constexpr
OutputIt
inclusive_scan(
InputIt first, |
(C++20 起) | |
template<
class
ExecutionPolicy, class
ForwardIt1, class
ForwardIt2 >
ForwardtIt2
inclusive_scan(
ExecutionPolicy&&
policy, ForwardIt1
first, |
(2) | (C++17 起) |
(3) | ||
template<
class
InputIt, class
OutputIt, class
BinaryOperation >
OutputIt inclusive_scan(
InputIt first, InputIt
last, |
(C++17
起) (C++20 前) |
|
template<
class
InputIt, class
OutputIt, class
BinaryOperation >
constexpr
OutputIt
inclusive_scan(
InputIt first, InputIt
last, |
(C++20 起) | |
template<
class
ExecutionPolicy, class
ForwardIt1, class
ForwardIt2,
class
BinaryOperation > |
(4) | (C++17 起) |
(5) | ||
template<
class
InputIt, class
OutputIt, class
BinaryOperation, class
T >
OutputIt inclusive_scan(
InputIt first, InputIt
last, OutputIt
d_first, |
(C++17
起) (C++20 前) |
|
template<
class
InputIt, class
OutputIt, class
BinaryOperation, class
T >
constexpr
OutputIt
inclusive_scan(
InputIt first, InputIt
last, OutputIt
d_first, |
(C++20 起) | |
template<
class
ExecutionPolicy, class
ForwardIt1, class
ForwardIt2,
class
BinaryOperation, class
T > |
(6) | (C++17 起) |
用 binary_op
(或对于重载 (1-2) 则是 std::plus<>() )对范围
[first, last)
计算包含性前缀和运算,以 init
为初始值(若提供),并将结果写入从 d_first
开始的范围。“包含性”意味着第 i 个输入元素包含于第 i 个和。
正式而言,通过每个 [d_first,
d_first + (last - first)) 中的迭代器 i
,以下列值赋值:
- 重载 (1-4) 为,对于每个区间 [first, first + (i -
d_first + 1)) 中的
j
,*j...
在binary_op
上的广义非交换和 - 重载 (5-6) 为,对于每个区间 [first, first + (i -
d_first + 1)) 中的
j
,init, *j...
在binary_op
上的广义非交换和
其中广义非交换和 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 必须符合老式向前迭代器
(LegacyForwardIterator)
的要求。
|
||
-
ForwardIt2 必须符合老式向前迭代器
(LegacyForwardIterator)
的要求。
|
||
-若不提供
init ,则 decltype(first)
的值类型必须为可移动构造
(MoveConstructible)
且 binary_op(*first, *first) 必须可转换成
decltype(first) 的值类型。
|
||
-
T (若提供 init ) 必须符合可移动构造
(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 个输入 (函数模板) |