跳到主要内容

std::inclusive_scan() 算法

// (1)
template< class InputIt, class OutputIt >
constexpr OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first );

// (2)
template< class InputIt, class OutputIt, class BinaryOperation >
constexpr OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first, BinaryOperation binary_op );

// (3)
template< class InputIt, class OutputIt, class BinaryOperation, class T >
constexpr OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first, BinaryOperation binary_op, T init );

// (4)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardtIt2 inclusive_scan( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation >
ForwardIt2 inclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
BinaryOperation binary_op );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation, class T >
ForwardIt2 inclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
BinaryOperation binary_op, T init );

使用 `binary_op`(或重载 **(1, 4)** 的 `std::plus<>()`)计算范围 [ `first` ; `last` ) 的前缀和操作,使用 `init` 作为初始值(如果提供),并将结果写入以 `d_first` 开头的范围。

"inclusive" 表示第 i 个输入元素包含在第 i 个和中。

求和操作可以按任意顺序执行,如果 binary_op 不具有结合性,则行为不确定。

形式上,对于 [ `d_first` ; `d_first + (last - first)` ) 中的每个迭代器 `i`,赋值的值为

  • ** (1, 2, 4, 5)** 对于 [ `first` ; `first + (i - d_first + 1)` ) 中每个 `j` 的 `binary_op` 的广义非交换和 `*j`...
  • ** (3, 6)** 对于 [ `first` ; `first + (i - d_first + 1))` 中每个 `j` 的 `binary_op` 的广义非交换和 `init`, `*j`...

其中广义非交换和 `GNSUM(op, a 1, ..., a N)` 定义如下

  • 如果 N = 1,则为 a1

  • 如果 `N > 1`,则对于任何 `K` 且 `1 < K + 1 = M ≤ N`,`op(GNSUM(op, a 1, ..., a K), GNSUM(op, a M, ..., a N))`

  • (4 - 6)(1 - 3) 相同,但根据策略执行。

    重载决议

    这些重载只有在 `std::is_execution_policy_v>` 为 `true` 时才参与重载决议。  (直到 C++20) `std::is_execution_policy_v>` 为 `true` 时才参与重载决议。  (自 C++20 起)

未定义行为

`binary_op` 不得使迭代器(包括结束迭代器)或子范围失效,也不得修改范围 [ `first`, `last` ) 或 [ `d_first`, `d_first + (last - first)` ) 中的元素。

否则,行为未定义

.

参数

first
last

要求和的元素范围。

d_first

目标范围的开始;可能等于 `first`。

init

广义和的初始值。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

binary_op

二元函数对象,将以未指定顺序应用于解引用输入迭代器的结果、其他 binary_op 的结果和 init

类型要求

InputItLegacyInputIterator
OutputItLegacyOutputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
  • 如果未提供 `init`,则 `decltype(first)` 的 `value_type` 必须是 `MoveConstructible`,并且 `binary_op(*first, *first)` 必须可转换为 `decltype(first)` 的值类型。
  • `T` (如果提供 `init`) 必须满足 `MoveConstructible` 的要求。以下表达式必须可转换为 `T`
    • binary_op(init, *first)
    • binary_op(init, init)
    • binary_op(*first, *first)

返回值

指向已写入的最后一个元素之后的元素的迭代器。

复杂度

O(last - first)binary_op 应用。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果在算法执行过程中调用的函数抛出异常且 `ExecutionPolicy` 是 **标准策略** 之一,则调用 `std::terminate`。对于任何其他 `ExecutionPolicy`,行为是 **实现定义的**.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

示例

Main.cpp
#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
本文源自 此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::inclusive_scan() 算法

// (1)
template< class InputIt, class OutputIt >
constexpr OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first );

// (2)
template< class InputIt, class OutputIt, class BinaryOperation >
constexpr OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first, BinaryOperation binary_op );

// (3)
template< class InputIt, class OutputIt, class BinaryOperation, class T >
constexpr OutputIt inclusive_scan( InputIt first, InputIt last, OutputIt d_first, BinaryOperation binary_op, T init );

// (4)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardtIt2 inclusive_scan( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );

// (5)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation >
ForwardIt2 inclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
BinaryOperation binary_op );

// (6)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryOperation, class T >
ForwardIt2 inclusive_scan( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first,
BinaryOperation binary_op, T init );

使用 `binary_op`(或重载 **(1, 4)** 的 `std::plus<>()`)计算范围 [ `first` ; `last` ) 的前缀和操作,使用 `init` 作为初始值(如果提供),并将结果写入以 `d_first` 开头的范围。

"inclusive" 表示第 i 个输入元素包含在第 i 个和中。

求和操作可以按任意顺序执行,如果 binary_op 不具有结合性,则行为不确定。

形式上,对于 [ `d_first` ; `d_first + (last - first)` ) 中的每个迭代器 `i`,赋值的值为

  • ** (1, 2, 4, 5)** 对于 [ `first` ; `first + (i - d_first + 1)` ) 中每个 `j` 的 `binary_op` 的广义非交换和 `*j`...
  • ** (3, 6)** 对于 [ `first` ; `first + (i - d_first + 1))` 中每个 `j` 的 `binary_op` 的广义非交换和 `init`, `*j`...

其中广义非交换和 `GNSUM(op, a 1, ..., a N)` 定义如下

  • 如果 N = 1,则为 a1

  • 如果 `N > 1`,则对于任何 `K` 且 `1 < K + 1 = M ≤ N`,`op(GNSUM(op, a 1, ..., a K), GNSUM(op, a M, ..., a N))`

  • (4 - 6)(1 - 3) 相同,但根据策略执行。

    重载决议

    这些重载只有在 `std::is_execution_policy_v>` 为 `true` 时才参与重载决议。  (直到 C++20) `std::is_execution_policy_v>` 为 `true` 时才参与重载决议。  (自 C++20 起)

未定义行为

`binary_op` 不得使迭代器(包括结束迭代器)或子范围失效,也不得修改范围 [ `first`, `last` ) 或 [ `d_first`, `d_first + (last - first)` ) 中的元素。

否则,行为未定义

.

参数

first
last

要求和的元素范围。

d_first

目标范围的开始;可能等于 `first`。

init

广义和的初始值。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

binary_op

二元函数对象,将以未指定顺序应用于解引用输入迭代器的结果、其他 binary_op 的结果和 init

类型要求

InputItLegacyInputIterator
OutputItLegacyOutputIterator
ForwardIt1
ForwardIt2
LegacyForwardIterator
  • 如果未提供 `init`,则 `decltype(first)` 的 `value_type` 必须是 `MoveConstructible`,并且 `binary_op(*first, *first)` 必须可转换为 `decltype(first)` 的值类型。
  • `T` (如果提供 `init`) 必须满足 `MoveConstructible` 的要求。以下表达式必须可转换为 `T`
    • binary_op(init, *first)
    • binary_op(init, init)
    • binary_op(*first, *first)

返回值

指向已写入的最后一个元素之后的元素的迭代器。

复杂度

O(last - first)binary_op 应用。

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果在算法执行过程中调用的函数抛出异常且 `ExecutionPolicy` 是 **标准策略** 之一,则调用 `std::terminate`。对于任何其他 `ExecutionPolicy`,行为是 **实现定义的**.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

示例

Main.cpp
#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
本文源自 此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。