std::inner_product
STD::内部[医]产品
Defined in header | | |
---|---|---|
template< class InputIt1, class InputIt2, class T > T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T value | (1) | |
template<class InputIt1, class InputIt2, class T, class BinaryOperation1, class BinaryOperation2> T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T value, BinaryOperation1 op1, BinaryOperation2 op2 | (2) | |
计算内积%28i.e。产品%29之和或在范围内执行订单地图/缩减操作[first1, last1)
范围从first2
...
1%29初始化累加器acc
初值init
然后用表达式修改它。acc = acc + *first1 * *first2
,然后使用表达式再次修改。acc = acc + *(first1+1) * *(first2+1)
等直至到达end1
.用于内置的+和%2A,计算这两个范围的内积。
2%29初始化累加器acc
初值init
然后用表达式修改它。acc = op1(acc, op2(*first1, *first2))
,然后使用表达式再次修改。acc = op1(acc, op2(*(first1+1), *(first2+1)))
等直至到达end1
...
op1 and op2 must not have side effects. | (until C++11) |
---|---|
op1 and op2 must not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved. | (since C++11) |
参数
first1, last1 | - | the first range of elements |
---|---|---|
first2 | - | the beginning of the second range of elements |
value | - | initial value of the sum of the products |
op1 | - | binary operation function object that will be applied. This "sum" function takes a value returned by op2 and the current value of the accumulator and produces a new value to be stored in the accumulator. The signature of the function should be equivalent to the following: Ret fun(const Type1 &a, const Type2 &b The signature does not need to have const &. The types Type1 and Type2 must be such that objects of types T and Type3 can be implicitly converted to Type1 and Type2 respectively. The type Ret must be such that an object of type T can be assigned a value of type Ret. |
op2 | - | binary operation function object that will be applied. This "product" function takes one value from each range and produces a new value. The signature of the function should be equivalent to the following: Ret fun(const Type1 &a, const Type2 &b The signature does not need to have const &. The types Type1 and Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. The type Ret must be such that an object of type Type3 can be assigned a value of type Ret. |
类型要求
-InputIt 1,InputIt 2必须符合InputIterator的要求。
-前进1,前进2必须符合先行者的要求。
-T必须符合CopyAssignable和CopyConstrucable的要求。
返回值
的最终价值acc
如上文所述。
可能的实施
第一版
*。
模板<类InputIt 1,类InputIt 2,类T>T内部[医]乘积%28InputIt1first 1,InputIt1last1,InputIt2first 2,T值%29{而%28first 1%21=last1%29{value=value+%2A前1%2A%2A前2;++first 1;++first 2;}返回值;}
第二版
模板<类InputIt 1,类InputIt 2,类T,类BinaryOperation 1,类BinaryOperation2>T内[医]乘积%28InputIt1first 1,InputIt1last1,InputIt2first 2,T值,BinaryOperation 1 OP1BinaryOperation2OP2%29{,而%28first 1%21=last1%29{value=op1%28,op2%28%2A首先,%2A前2%29%29;++first 1;++first 2;}返回值;}
注记
这个算法的可并行版本,std::transform_reduce
,要求op1
和op2
交换和联想,但是std::inner_product
没有这样的要求,并且总是按照给定的顺序执行操作。
例
二次
#include <numeric>
#include <iostream>
#include <vector>
#include <functional>
int main()
{
std::vector<int> a{0, 1, 2, 3, 4};
std::vector<int> b{5, 4, 2, 3, 1};
int r1 = std::inner_product(a.begin(), a.end(), b.begin(), 0
std::cout << "Inner product of a and b: " << r1 << '\n';
int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0,
std::plus<>(), std::equal_to<>()
std::cout << "Number of pairwise matches between a and b: " << r2 << '\n';
}
二次
产出:
二次
Inner product of a and b: 21
Number of pairwise matches between a and b: 2
二次
另见
transform_reduce (C++17) | applies a functor, then reduces out of order (function template) |
---|---|
accumulate | sums up a range of elements (function template) |
partial_sum | computes the partial sum of a range of elements (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。