C++
算法 | Algorithm

std::reduce

性病:减少

Defined in header
template<class InputIt> typename std::iterator_traits<InputIt>::value_type reduce( InputIt first, InputIt last(1)(since C++17)
template<class ExecutionPolicy, class ForwardIt> typename std::iterator_traits<ForwardIt>::value_type reduce( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last(2)(since C++17)
template<class InputIt, class T> T reduce(InputIt first, InputIt last, T init(3)(since C++17)
template<class ExecutionPolicy, class ForwardIt, class T> T reduce(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, T init(4)(since C++17)
template<class InputIt, class T, class BinaryOp> T reduce(InputIt first, InputIt last, T init, BinaryOp binary_op(5)(since C++17)
template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOp> T reduce(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, T init, BinaryOp binary_op(6)(since C++17)

1%29reduce(first, last, typenamestd::iterator_traits<InputIt>::value_type{})

3%29reduce(first, last, init,std::plus<>())

5%29缩小射程。[首先,最后%29,可能以未指定的方式排列和聚合,以及初始值init过关binary_op...

2,4,6%29等于%281,3,5%29,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

如果行为是不确定的,则行为是不确定的。binary_op不是联想的也不是交换的。

如果binary_op中的任何元素或使任何迭代器无效。第一;最后,包括末端迭代器。

参数

first, last-the range of elements to apply the algorithm to
init-the initial value of the generalized sum
policy-the execution policy to use. See execution policy for details.
binary_op-binary FunctionObject that will be applied in unspecified order to the result of dereferencing the input iterators, the results of other binary_op and init.

类型要求

-输入必须符合输入器的要求。

---。

-T必须符合可移动建筑的要求。和二进制[医]执行部分第28段,%2A第29%,二进制[医]执行部分第28段%2A首先,init%29,二进制[医]OP%28 init、init%29和二进制[医]执行部分第28段%2A首先,%2A第一%29必须转换为T。

返回值

广义和init*first,,,*(first+1)..。*(last-1)过关binary_op,,,

其中广义和GSUM%28 op,a

1,...a

N%29的定义如下:

  • 如果N=1,a 一

  • 如果N>1,OP%28 GSUM%28 OP,b

1,...,b

K%29,GSUM%28 OP,b

M,...,b

N%29%29

- b1,...,b N可以是A1,...,an和 -1<K+1=M≤N

换句话说,reduce表现得像std::accumulate但范围的元素可以按任意顺序分组和重新排列。

复杂性

O%28最后-首%29binary_op...

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。

  • 如果算法不能分配内存,std::bad_alloc被扔了。

注记

如果范围是空的,init被返回,未经修改。

减缩与平行比较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

二次

另见

accumulatesums up a range of elements (function template)
transformapplies a function to a range of elements (function template)
transform_reduce (C++17)applies a functor, then reduces out of order (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/Reduce