Share via


accumulate

透過計算執行部分和計算所有項目的總和在指定的範圍加入一些初始值或計算使用指定的二進位運算的方式取得執行部分結果的總和之外。

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
    輸入定址的 Iterator 在根據指定的二進位運算要加總或合併的範圍的第一個項目。

  • _Last
    輸入定址的 Iterator 以在最後一個項目以外的位置上指定的二進位運算要加總或合併的範圍的最後一個項目在中逐一查看的累積實際包含。

  • _Val
    每個元素依次加入或結合根據指定的二進位運算的初始值。

  • _Binary_op
    要套用至指定的範圍和先前的應用程式之結果的每個項目的二進位運算。

傳回值

_Val 和所有項目的總和在指定範圍中第一個樣板函式的或,第二個樣板函式,套用的二元運算的結果所指定,而不是指定的作業,對 (PartialResult, *Iter),其中 PartialResult 是作業和 Iter 的上一個應用程式的結果是指向一個項目的 Iterator 在範圍。

備註

原始值是否為將具有定義完善的結果時,這個範圍是空的時,在這種情況下, _Val 傳回情況下。 二進位運算不需要是聯結或可交換的。 結果初始化為初始值 _Val 然後 結果 = _Binary_op (因此, *****Iter) 傳遞範圍重複計算, Iter 是指向範圍中後續項目的 Iterator。 範圍必須是有效的,而且複雜是線性與範圍的大小。 二元運算子的傳回型別必須可以轉換為 [型別] 在反覆項目期間確保關閉。

範例

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

需求

標題: <的值>

命名空間: std

請參閱

參考

accumulate、copy 和 vector::push_back

標準樣板程式庫