次の方法で共有


accumulate

初期値を含めて、指定した範囲のすべての要素の合計を逐次的な部分の合計を計算することで計算します。または、同様に指定以外の合計 2 進操作から派生した継続的に、一部の結果の結果を計算します。

template<class InputIterator, class Type>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val
   );
template<class InputIterator, class Type, class BinaryOperation>
   Type accumulate(
      InputIterator _First, 
      InputIterator _Last, 
      Type _Val, 
      BinaryOperation _Binary_op
   );

パラメーター

  • _First
    指定した 2 進操作に従って合計されるか、または結合された範囲内の先頭の要素を示す入力反復子。

  • _Last
    最後の要素を 1 個以上の位置にある、指定した 2 進操作に従って合計されるか、または結合された範囲内の最後の要素を示す入力反復子は、反復された累積に実際に含まれています。

  • _Val
    各リボン要素が 2 桁に従って操作と順に追加するか、または結合する初期値。

  • _Binary_op
    前のアプリケーションの指定範囲と結果の各要素に適用される 2 進操作。

戻り値

PartialResult が 演算および Iter の前のアプリケーションの結果であるに The sum 一つ目のテンプレート関数の指定範囲の _Val とすべての要素は、2 番目のテンプレート関数の、日付の操作ではなく、に (PartialResult の *Iter) 指定した 2 進操作を適用した結果の範囲の要素を指す反復子です。

解説

初期値が返された場合は _Val 範囲が空の場合、正しく定義された結果になることを保証します。2 進操作は、結合性または可換である必要はありません。結果は、初期値 _Val に初期化され、次に 結果_Binary_op = (結果、**[Hz]**Iter) は Iter が範囲の連続する要素を指す反復子であるスコープを通じて繰り返し計算されます。スコープは有効である必要があり、複雑さは範囲のサイズと直線的です。二項演算子の戻り値の型は、イテレーション中にロックアウトを確認するに [種類] に変換できる必要があります。

使用例

// numeric_accum.cpp
// compile with: /EHsc
#include <vector>
#include <numeric>
#include <functional>
#include <iostream>

int main( )
{
   using namespace std;

   vector <int> v1, v2(20);
   vector <int>::iterator iter1, iter2;

   int i;
   for (i = 1; i < 21; i++)
   {
      v1.push_back(i);
   }

   cout << "The original vector v1 is:\n ( " ;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
      cout << *iter1 << " ";
   cout << ")." << endl;

   // The first member function for the accumulated sum
   int total;
   total = accumulate(v1.begin(), v1.end(), 0);

   cout << "The sum of the integers from 1 to 20 is: "
        << total << "." << endl;

   // Constructing a vector of partial sums
   int j = 0, partotal;
   for (iter1 = v1.begin(); iter1 != v1.end(); iter1++)
   {
      partotal = accumulate(v1.begin(), iter1 + 1, 0);
      v2[j] = partotal;
      j++;
   }

   cout << "The vector of partial sums is:\n ( " ;
   for (iter2 = v2.begin(); iter2 != v2.end(); iter2++)
      cout << *iter2 << " ";
   cout << ")." << endl << endl;

   // The second member function for the accumulated product
   vector <int> v3, v4(10);
   vector <int>::iterator iter3, iter4;

   int s;
   for (s = 1; s < 11; s++)
   {
      v3.push_back(s);
   }

   cout << "The original vector v3 is:\n ( " ;
   for (iter3 = v3.begin(); iter3 != v3.end(); iter3++)
      cout << *iter3 << " ";
   cout << ")." << endl;

   int ptotal;
   ptotal = accumulate(v3.begin(), v3.end(), 1, multiplies<int>());

   cout << "The product of the integers from 1 to 10 is: "
        << ptotal << "." << endl;

   // Constructing a vector of partial products
   int k = 0, ppartotal;
   for (iter3 = v3.begin(); iter3 != v3.end(); iter3++) {
      ppartotal = accumulate(v3.begin(), iter3 + 1, 1, multiplies<int>());
      v4[k] = ppartotal;
      k++;
   }

   cout << "The vector of partial products is:\n ( " ;
   for (iter4 = v4.begin(); iter4 != v4.end(); iter4++)
      cout << *iter4 << " ";
   cout << ")." << endl;
}
  
  
  
  
  
  

必要条件

ヘッダー: <numeric>

名前空間: std

参照

関連項目

accumulate, copy, と vector::push_back

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