首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::transform_reduce

Defined in header <numeric>

?

?

template<class InputIt1, class InputIt2, class T> T transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init);

(1)

(since C++17)

template <class InputIt1, class InputIt2, class T, class BinaryOp1, class BinaryOp2> T transform_reduce(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOp1 binary_op1, BinaryOp2 binary_op2);

(2)

(since C++17)

template<class InputIt, class T, class BinaryOp, class UnaryOp> T transform_reduce(InputIt first, InputIt last, T init, BinaryOp binop, UnaryOp unary_op);

(3)

(since C++17)

template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T> T transform_reduce(ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, T init);

(4)

(since C++17)

template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T, class BinaryOp1, class BinaryOp2> T transform_reduce(ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, T init, BinaryOp1 binary_op1, BinaryOp2 binary_op2);

(5)

(since C++17)

template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOp, class UnaryOp> T transform_reduce(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, T init, BinaryOp binary_op, UnaryOp unary_op);

(6)

(since C++17)

1%29相当于transform_reduce(first1, last1, first2, init, std::plus<>(), std::multiplies<>());,有效地并行化了默认的版本。std::inner_product

2%29申请binary_op2范围内的每一对元素。[first; last)范围从first2并减少结果%28,可能以未指定的方式与初始值一起被置换和聚合。init过关binary_op1

3%29申请unary_op范围内的每个元素。[首先,最后的%29,并减少结果%28,可能以未指定的方式与初始值一起被置换和聚合。init过关binary_op...

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

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

如果unary_op,,,binary_op,,,binary_op1,或binary_op2修改任何元素或使输入范围中的任何迭代器无效,包括它们的结束迭代器。

参数

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.

unary_op

-

unary FunctionObject that will be applied to each element of the input range. The return type must be acceptable as input to binary_op

binary_op

-

binary FunctionObject that will be applied in unspecified order to the results of unary_op, the results of other binary_op and init.

类型要求

-T必须符合可移动建筑的要求,才能使用重载%283,6%29。以及表达式二进制的结果。[医]OP%28 init,一元[医]执行部分第28段%2A第1%29%29,二进制[医]执行部分%28一元[医]执行部分第28段%2A第一个%29,init%29,二进制[医]OP%28 init、init%29和二进制[医]执行部分%28一元[医]执行部分第28段%2A第一%29,一元[医]执行部分第28段%2A第一%29%29必须转换为T

-T必须符合可移动建筑的要求,才能使用过载%282,5%29。以及表达式二进制的结果。[医]执行部分1%28 init,二进制[医]执行部分2%28%2A首先,%2A前2%29%29,二进制[医]OP1%28二进制[医]执行部分2%28%2A首先,%2A前2%29,init%29,二进制[医]OP1%28 init,init%29,和二进制[医]OP1%28二进制[医]执行部分2%28%2A首先,%2A前2%29,二进制[医]执行部分2%28%2A首先,%2A前2%29%29必须转换为T

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

---。

返回值

2%29广义和initbinary_op2(*first,*first2),,,binary_op2(*(first+1),*(first2+1))...完毕binary_op1

3%29广义和initunary_op(*first),,,unary_op(*(first+1))..。unary_op(*(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

代码语言:txt
复制
-  b1,...,b
N可以是A1,...,an和
-1<K+1=M≤N

换句话说,一元的结果[医]OP或二进制[医]执行部分第1段可按任意顺序分组和安排。

复杂性

1,2,4,5%29 O%28last1-头1%29份申请binary_op1binary_op2...

3,6%29 O%28最后一次申请unary_opbinary_op...

例外

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

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

注记

在一元二进制过载%283,6%29,unary_op不适用于init...

如果first == lastfirst1 == last1,,,init被返回,未经修改。

变换[医]可用于并行化std::inner_product*

二次

代码语言:javascript
复制
#include <vector>
#include <functional>
#include <iostream>
#include <numeric>
#include <execution>
 
int main()
{
    std::vector<double> xvalues(10007, 1.0), yvalues(10007, 1.0);
 
    double result = std::transform_reduce(
        std::execution::par,
        xvalues.begin(), xvalues.end()
        yvalues.begin(), 0.0,
    );
    std::cout << result << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
10007

二次

另见

accumulate

sums up a range of elements (function template)

transform

applies a function to a range of elements (function template)

reduce (C++17)

similar to std::accumulate, except out of order (function template)

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com