partial_sum
通过 第i元素在目标范围的 第i元素计算在输入范围的一系列总和从第一个元素并存储每个这样的总和的结果或计算总和操作将另一个指定的二元运算一个通用程序的结果。
template<class InputIterator, class OutIt>
OutputIterator partial_sum(
InputIterator _First,
InputIterator _Last,
OutputIterator _Result
);
template<class InputIterator, class OutIt, class Fn2>
OutputIterator partial_sum(
InputIterator _First,
InputIterator _Last,
OutputIterator _Result,
BinaryOperation _Binary_op
);
参数
_First
解决输入的迭代器在基于指定的二元运算部分计算或合并范围的第一个元素。_Last
解决输入的迭代器在基于是在最终元素之外的某个位置的指定二元运算部分计算或合并的范围的最后一个组件重复的累计实际上由了。_Result
解决输出的迭代器第一个元素目标范围要存储的位置和部分或指定的操作的结果序列。_Binary_op
将应用于和替换操作的部分中的和过程的通用操作的二元运算。
返回值
解决输出的迭代器目标范围的末尾: _Result + (_Last -_First),
备注
输出迭代器 _Result 允许为迭代器和输入迭代器 _First相同,因此,部件和可以就地计算。
为值 的1序列,则2,则3,在输入范围,第一个模板函数在目标范围存储顺序部分中,指定 第i元素((1 + *)*2+ *)3)*我。
为值 的1序列,则2,则3,在输入范围,第二个模板函数在目标范围存储顺序部分中,指定第i元素(_Binary_op(1*)2_Binary_op)3)*我。
不需要该二进制操作 _Binary_op 是或关联的或可交换,因为操作顺序应用完全指定。
partial_sum 有两个相关的窗体:
如果通过经过检查的迭代器某个 partial_sum的窗体,则得到检查的迭代器行为。 如果将未检查的迭代器,可以获得未经检查的行为。 有关更多信息,请参见 经过检查的迭代器。
示例
// numeric_partial_sum.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>
int main( )
{
using namespace std;
vector<int> V1( 10 ), V2( 10 );
vector<int>::iterator VIter1, VIter2, VIterend, VIterend2;
list <int> L1;
list <int>::iterator LIter1, LIterend;
int t;
for ( t = 1 ; t <= 10 ; t++ )
{
L1.push_back( t );
}
cout << "The input list L1 is:\n ( " ;
for ( LIter1 = L1.begin( ) ; LIter1 != L1.end( ) ; LIter1++ )
cout << *LIter1 << " ";
cout << ")." << endl;
// The first member function for the partial sums of
// elements in a list output to a vector
VIterend = partial_sum ( L1.begin ( ) , L1.end ( ) ,
V1.begin ( ) );
cout << "The output vector containing the partial sums is:\n ( " ;
for ( VIter1 = V1.begin( ) ; VIter1 != VIterend ; VIter1++ )
cout << *VIter1 << " ";
cout << ")." << endl;
// The second member function used to compute
// the partial product of the elements in a list
VIterend2 = partial_sum ( L1.begin ( ) , L1.end ( ) , V2.begin ( ) ,
multiplies<int>( ) );
cout << "The output vector with the partial products is:\n ( " ;
for ( VIter2 = V2.begin( ) ; VIter2 != VIterend2 ; VIter2++ )
cout << *VIter2 << " ";
cout << ")." << endl;
// Computation of partial sums in place
LIterend = partial_sum ( L1.begin ( ) , L1.end ( ) , L1.begin ( ) );
cout << "The in place output partial_sum list L1 is:\n ( " ;
for ( LIter1 = L1.begin( ) ; LIter1 != LIterend ; LIter1++ )
cout << *LIter1 << " ";
cout << ")." << endl;
}
Output
The input list L1 is:
( 1 2 3 4 5 6 7 8 9 10 ).
The output vector containing the partial sums is:
( 1 3 6 10 15 21 28 36 45 55 ).
The output vector with the partial products is:
( 1 2 6 24 120 720 5040 40320 362880 3628800 ).
The in place output partial_sum list L1 is:
( 1 3 6 10 15 21 28 36 45 55 ).
要求
标头: <numeric>
命名空间: std