Namespaces
Variants

std:: inclusive_scan

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <numeric>
template < class InputIt, class OutputIt >

OutputIt inclusive_scan ( InputIt first, InputIt last,

OutputIt d_first ) ;
(1) (C++17 起)
(C++20 起为 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2 >
ForwardIt2 inclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first ) ;
(2) (C++17 起)
template < class InputIt, class OutputIt, class BinaryOp >

OutputIt inclusive_scan ( InputIt first, InputIt last,

OutputIt d_first, BinaryOp op ) ;
(3) (C++17 起)
(C++20 起为 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2, class BinaryOp >
ForwardIt2 inclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, BinaryOp op ) ;
(4) (C++17 起)
template < class InputIt, class OutputIt,

class BinaryOp, class T >
OutputIt inclusive_scan ( InputIt first, InputIt last,

OutputIt d_first, BinaryOp op, T init ) ;
(5) (C++17 起)
(C++20 起为 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2,
class BinaryOp, class T >
ForwardIt2 inclusive_scan ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 d_first, BinaryOp op, T init ) ;
(6) (C++17 起)
1) 等价于 inclusive_scan ( first, last, d_first, std:: plus <> ( )
3) 使用 op 计算包含性前缀和。
对于区间 [ 0 , std:: distance ( first, last ) ) 内的每个整数 i ,按顺序执行以下操作:
  1. 创建一个由 [ first , iter ] 范围内的元素按顺序构成的序列,其中 iter first 的第 i th 个后继迭代器。
  2. 计算该序列在 op 操作下的广义非交换和。
  3. 将结果赋值给 * dest ,其中 dest d_first 的第 i th 个后继迭代器。
5) (3) 相同,但每个创建的序列由 init 后接按顺序排列的 [ first , iter ] 元素组成。
2,4,6) (1,3,5) 相同,但根据 policy 执行。
这些重载仅在满足以下所有条件时参与重载决议:

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 广义非交换和 定义如下:

  • 如果序列只有一个元素,则总和为该元素的值。
  • 否则,按顺序执行以下操作:
  1. 从序列中选择任意两个相邻元素 elem1 elem2
  2. 计算 binary_op ( elem1, elem2 ) 并将序列中的这两个元素替换为计算结果。
  3. 重复步骤1和2,直到序列中仅剩一个元素。


给定 binary_op 作为实际的二元操作:

  • 如果 binary_op 不具有结合性(例如浮点数加法),则结果将是非确定性的。
  • 对于重载 (1-4) ,若 binary_op ( * first, * first ) 无法转换为 decltype ( first ) 值类型 ,则程序非良构。
  • 对于重载 (5,6) ,若下列任一值无法转换为 T 类型,则程序非良构:
  • binary_op ( init, * first )
  • binary_op ( init, init )
  • binary_op ( * first, * first )
  • 若满足以下任一条件,则行为未定义:
  • 对于重载 (1-4) decltype ( first ) 的值类型不满足 MoveConstructible 要求。
  • 对于重载 (5,6) 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 )

1,2) O(N) std:: plus <> ( ) 的应用。
3-6) 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

参见

计算范围内相邻元素的差值
(函数模板)
对元素范围进行求和或折叠操作
(函数模板)
计算元素范围的部分和
(函数模板)
应用可调用对象后计算包含性扫描
(函数模板)
类似于 std::partial_sum ,但从第 i 个和中排除第 i 个输入元素
(函数模板)