共用方式為


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_op2 *b)*1 _Binary_op1 (2 _Binary_op2 *b)*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 ).

需求

標題: <的值>

命名空間: std

請參閱

參考

inner_product (STL 範例)

標準樣板程式庫