共用方式為


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
    處理輸入的 Iterator 在內容區域或通用的區域與第二個範圍是計算的第一個範圍的第一個項目。

  • _Last1
    處理輸入的 Iterator 在內容區域或通用的區域與第二個範圍是計算的第一個範圍的最後一個項目。

  • _First2
    處理輸入的 Iterator 在內容區域或通用的區域與第一個範圍是計算的第二個範圍中第一個項目。

  • _Val
    內容區域或通用內的範圍之間要加入的原始值。

  • _Binary_op1
    取代總和的內的作業的二進位作業套用至內容區域的項目通用的產品。

  • _Binary_op2
    取代中的項目作業的二進位運算內區域的一般化相乘。

傳回值

第 10% 成員函式傳回項目產品的總和並將指定的初始值。 因此的值 ai和雙精度浮點數 範圍,則會傳回:

_Val + ( a1 、 *b)*1 + ( a2 、 *b)*2 +

逐一查看取代 _Val 以 _Val + (*a我 * *b)我 。

第二 + 成成員函式會傳回:

     _Val _Binary_op1 (1*_Binary_op2b1 ) _Binary_op1 (2_Binary_op2b*2 ) _Binary_op1

逐一查看取代 _Val 以 _Val _Binary_op1 (*a我 _Binary_op2 *b)我 。

備註

原始值可以確保將具有完整定義的結果,在此範圍是空的時候,在這種情況下, _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 ).

需求

標題: <numeric>

命名空間: std

請參閱

參考

inner_product (STL Samples)

標準樣板程式庫