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
指定二項演算に従って Aggregate、または結合される範囲内の先頭の要素を示す入力反復子。_Last
最後の要素を 1 個以上の位置を指定二項演算に従って Aggregate、または結合した範囲の最後の要素を指定する入力反復子は繰り返された累積に含まれています。_Val
各要素を指定二項演算に従ってと順に追加または結合される初期値。_Binary_op
前のアプリケーションの指定範囲と結果の各要素に適用される二項演算。
戻り値
PartialResult が 操作と Iter の前にアプリケーションの結果である場合は最初のテンプレート関数の指定された範囲の _Val とすべての要素の合計が、2 番目のテンプレート関数は、合計操作ではなく、(PartialResult の *Iter) に指定された二項演算を適用した結果は span 要素を指す反復子です。
解説
初期値は _Val が返された範囲が空の場合に定義された結果になることが保証されます。 二項演算は結合または可換性である必要はありません。 結果は、初期値 _Val に初期化され、結果_Binary_op = (結果、###*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;
}
必要条件
数値ヘッダー: <>
名前空間: std