std::reduce
来自cppreference.com
定义于头文件 <numeric> | ||
template<class InputIt> typenamestd::iterator_traits<InputIt>::value_type reduce( | (1) | (C++17 起) |
template<class ExecutionPolicy, class ForwardIt> typenamestd::iterator_traits<ForwardIt>::value_type reduce( | (2) | (C++17 起) |
template<class InputIt, class T> T reduce(InputIt first, InputIt last, T init); | (3) | (C++17 起) |
template<class ExecutionPolicy, class ForwardIt, class T> T reduce(ExecutionPolicy&& policy, | (4) | (C++17 起) |
template<class InputIt, class T, class BinaryOp> T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op); | (5) | (C++17 起) |
template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOp> T reduce(ExecutionPolicy&& policy, | (6) | (C++17 起) |
1) 同 reduce(first, last, typenamestd::iterator_traits<InputIt>::value_type{})
3) 同 reduce(first, last, init, std::plus<>())
5) 在
binary_op
上以初值 init
规约范围 [first; last) ,可能以未指定方式排序聚合。 2,4,6) 同 (1,3,5) ,但按照
policy
执行。此重载仅若std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true才参与重载决议若 binary_op
非结合或非交换,则行为非确定。
若 binary_op
修改 [first; last] 中任何元素或非法化范围中任何迭代器,含尾迭代器,则行为未定义。
目录 |
[编辑]参数
first, last | - | 要应用算法的元素范围 |
init | - | 广义和的初值 |
policy | - | 使用的执行策略。细节见执行策略。 |
binary_op | - | 将以未指定顺序应用于解引用输入迭代器结果、其他 binary_op 结果及 init 上的二元函数对象 (FunctionObject ) 。 |
类型要求 | ||
-InputIt 必须满足 InputIterator 的要求。 | ||
-ForwardIt 必须满足 ForwardIterator 的要求。 | ||
-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) | 应用函数对象,然后以乱序规约 (函数模板) |