inner_product
计算两范围元素的总和产品并将其添加到的指定初始值或计算总和和产品二进制操作由其他指定的二进制操作替换通用过程的结果。
template<class InputIterator1, class InputIterator2, class Type>
Type inner_product(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
Type _Val
);
template<class InputIterator1, class InputIterator2, class Type,
class BinaryOperation1, class BinaryOperation2>
Type inner_product(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
Type _Val,
BinaryOperation1 _Binary_op1,
BinaryOperation2 _Binary_op2
);
参数
_First1
在解决内积或泛化内积带第二个范围的是计算的第一范围的输入迭代器的第一个元素。_Last1
在解决内积或泛化内积带第二个范围的是计算的第一范围的输入迭代器最后一个元素。_First2
在解决内积或泛化内积带第一范围的是计算的第二个值范围的输入迭代器的第一个元素。_Val
内积或泛化内积在范围之间将的初始值。_Binary_op1
替换和的操作内积的二进制操作应用于内积泛化元素的产品。_Binary_op2
替换内积元素操作的操作中内积二进制泛化相乘。
返回值
第一个成员函数返回产品元素的总和并向该属性中指定的初始值。 因此对于范围值 ai和双精度型,它返回:
(+_Val用1 * *b)*1 + (* *、*2 + b2 )
通过迭代 _Val 替换与 _Val + *ai (* *b)i。
第二个成员函数返回:
_Val _Binary_op1 (1 _Binary_op2 *b)*1 _Binary_op1 (2 _Binary_op2 *b)*2 _Binary_op1
通过迭代 _Val 替换与 _Val _Binary_op1 (*ai _Binary_op2 *b)i。
备注
初始值确保具有显式定义的结果,在范围为空时,在这种情况下,_Val 返回条件下。 二进制操作不是必需或关联可交换的。 范围绑定有效,而复杂是线性的使用范围的大小。 二元运算符的返回类型必须可转换为 类型 迭代过程确保关闭。
示例
// numeric_inner_prod.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>
int main()
{
using namespace std;
vector <int> v1, v2(7), v3(7);
vector <int>::iterator iter1, iter2, iter3;
int i;
for (i = 1; i <= 7; i++)
{
v1.push_back(i);
}
cout << "The original vector v1 is:\n ( " ;
for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
cout << *iter1 << " ";
cout << ")." << endl;
list <int> l1, l2(7);
list <int>::iterator lIter1, lIter2;
int t;
for (t = 1; t <= 7; t++)
{
l1.push_back(t);
}
cout << "The original list l1 is:\n ( " ;
for (lIter1 = l1.begin(); lIter1 != l1.end(); lIter1++)
cout << *lIter1 << " ";
cout << ")." << endl;
// The first member function for the inner product
int inprod;
inprod = inner_product(v1.begin(), v1.end(), l1.begin(), 0);
cout << "The inner_product of the vector v1 and the list l1 is: "
<< inprod << "." << endl;
// Constructing a vector of partial inner_products between v1 & l1
int j = 0, parinprod;
for (iter1 = v1.begin(); iter1 != v1.end(); iter1++) {
parinprod = inner_product(v1.begin(), iter1 + 1, l1.begin(), 0);
v2[j] = parinprod;
j++;
}
cout << "Vector of partial inner_products between v1 & l1 is:\n ( " ;
for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
cout << *iter2 << " ";
cout << ")." << endl << endl;
// The second member function used to compute
// the product of the element-wise sums
int inprod2;
inprod2 = inner_product (v1.begin(), v1.end(),
l1.begin(), 1, multiplies<int>(), plus<int>());
cout << "The sum of the element-wise products of v1 and l1 is: "
<< inprod2 << "." << endl;
// Constructing a vector of partial sums of element-wise products
int k = 0, parinprod2;
for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
{
parinprod2 =
inner_product(v1.begin(), iter1 + 1, l1.begin(), 1,
multiplies<int>(), plus<int>());
v3[k] = parinprod2;
k++;
}
cout << "Vector of partial sums of element-wise products is:\n ( " ;
for (iter3 = v3.begin(); iter3 != v3.end(); iter3++)
cout << *iter3 << " ";
cout << ")." << endl << endl;
}
Output
The original vector v1 is:
( 1 2 3 4 5 6 7 ).
The original list l1 is:
( 1 2 3 4 5 6 7 ).
The inner_product of the vector v1 and the list l1 is: 140.
Vector of partial inner_products between v1 & l1 is:
( 1 5 14 30 55 91 140 ).
The sum of the element-wise products of v1 and l1 is: 645120.
Vector of partial sums of element-wise products is:
( 2 8 48 384 3840 46080 645120 ).
要求
数字 <的页眉: >
命名空间: std