std::reduce
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<numeric>
定义
|
||
(1) | ||
template<class InputIt>
typename std::iterator_traits<InputIt>::value_type reduce( |
(C++17 起) (C++20 前) |
|
template<class InputIt>
constexpr typename std::iterator_traits<InputIt>::value_type reduce( |
(C++20 起) | |
template<class ExecutionPolicy,
class ForwardIt>
typename std::iterator_traits<ForwardIt>::value_type reduce( |
(2) | (C++17 起) |
(3) | ||
template<class InputIt, class T>
T reduce(InputIt first, InputIt last, T init); |
(C++17 起) (C++20 前) |
|
template<class InputIt, class T>
constexpr T reduce(InputIt first, InputIt last, T init); |
(C++20 起) | |
template<class ExecutionPolicy,
class ForwardIt, class
T>
T reduce(ExecutionPolicy&& policy, |
(4) | (C++17 起) |
(5) | ||
template<class InputIt, class T, class BinaryOp>
T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op); |
(C++17 起) (C++17 前) |
|
template<class InputIt, class T, class BinaryOp>
constexpr T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op); |
(C++17 起) | |
template<class ExecutionPolicy,
class ForwardIt, class T,
class BinaryOp>
T reduce(ExecutionPolicy&& policy, |
(6) | (C++17 起) |
binary_op
上以初值 init
规约范围
[first; last) ,可能以未指定方式排序聚合。 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
非结合或非交换,则行为非确定。
若 binary_op
修改 [first;
last] 中任何元素或非法化范围中任何迭代器,含尾迭代器,则行为未定义。
参数
first, last | - | 要应用算法的元素范围 |
init | - | 广义和的初值 |
policy | - | 使用的执行策略。细节见执行策略。 |
binary_op | - | 将以未指定顺序应用于解引用输入迭代器结果、其他 binary_op 结果及 init 上的二元函数对象 (FunctionObject)
。
|
类型要求 | ||
-InputIt 必须符合老式输入迭代器 (LegacyInputIterator)
的要求。
|
||
-ForwardIt 必须符合老式向前迭代器 (LegacyForwardIterator)
的要求。
|
||
-T 必须符合可移动构造
(MoveConstructible)
的要求。而且 binary_op(init, *first) 、 binary_op(*first, init)
、 binary_op(init, init) 及 binary_op(*first, *first) 必须可转换到
T 。
|
返回值
init
及 *first
、 *(first+1)
、…… *(last-1)
在
binary_op
上的广义和,
其中广义和 GSUM(op, a
1, ..., a
N)
定义如下:
- 若 N=1 ,则为 a
1 - 若 N > 1 ,则为 op(GSUM(op, b
1, ..., b
K), GSUM(op, b
M, ..., b
N)) ,其中
-
- b
1, ..., b
N 可以是任何 a1, ..., aN 的排列,且 - 1 < K+1 = M ≤ N
- b
换言之, reduce
表现类似 std::accumulate ,除了范围中的元素可能以任意顺序分组并重排。
复杂度
O(last - first) 次应用
binary_op
.
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy
,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
注解
若范围为空,则返回不修改的 init
。
示例
reduce 与 std::accumulate 间并行的比较:
#include <iostream> #include <chrono> #include <vector> #include <numeric> #include <execution> int main() { std::vector<double> v(10'000'007, 0.5); { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::accumulate(v.begin(), v.end(), 0.0); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << std::fixed << "std::accumulate result " << result << " took " << ms.count() << " ms\n"; } { auto t1 = std::chrono::high_resolution_clock::now(); double result = std::reduce(std::execution::par, v.begin(), v.end()); auto t2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> ms = t2 - t1; std::cout << "std::reduce result " << result << " took " << ms.count() << " ms\n"; } }
可能的输出:
std::accumulate result 5000003.50000 took 12.7365 ms std::reduce result 5000003.50000 took 5.06423 ms
参阅
对一个范围内的元素求和 (函数模板) |
|
将一个函数应用于某一范围的各个元素,并在目标范围存储结果 (函数模板) |
|
(C++17)
|
应用一个函数对象,然后以乱序规约 (函数模板) |