次の方法で共有


inner_product

2 スコープ的に製品の要素の合計を計算し、指定された初期値に追加するか、合計、および製品 2 進操作は他の指定、2 進操作と置換される汎用的な手順の結果を計算します。

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
    2 番目の範囲を持つ内部製品または製品汎用的な内部計算対象となる最初の範囲内の先頭の要素を示す入力反復子。

  • _Last1
    2 番目の範囲を持つ内部製品または製品汎用的な内部計算対象となる最初の範囲内の最後の要素を示す入力反復子。

  • _First2
    最初の範囲を持つ内部製品または製品汎用的な内部計算対象となる 2 番目の範囲内の先頭の要素を示す入力反復子。

  • _Val
    範囲間の内部製品または汎用的な内部製品が追加される初期値。

  • _Binary_op1
    合計の内部製品 2 進操作と置換操作は内部製品の原則の要素的に製品に適用しました。

  • _Binary_op2
    製品要素内部的に 2 進操作と置換操作は内部製品の原則にインクリメントされます。

戻り値

一つ目のメンバー関数は要素的に製品の合計を返し、指定された初期値を追加します。この値 aiBiの範囲の場合、が返されます:

_Val + * ( a1b) 1 + ( a2b) 2 + *

繰り返し _Val と _Val を置き換えることで、+ *aI *b (*) I。

2 番目のメンバー関数はを返します:

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

繰り返し _Val _Binary_op1 (*aI _Binary_op2 の *b) と _Val を置き換えることによってI。

解説

初期値が返された場合は _Val 範囲が空の場合、正しく定義された結果があることを確認します。2 進操作は、結合性または可換である必要はありません。スコープは有効である必要があり、複雑さは範囲のサイズと直線的です。二項演算子の戻り値の型は、イテレーション中にロックアウトを確認するに [種類] に変換できる必要があります。

使用例

// 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;
}

出力

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)

標準テンプレート ライブラリ