++


partial_sum()


template < class InputIterator, class OutputIterator >

OutputIterator

partial_sum(

InputIterator first, InputIterator last,

OutputIterator result );

template < class InputIterator, class OutputIterator,

class BinaryOperation >

OutputIterator

partial_sum(

InputIterator first, InputIterator last,

OutputIterator result, BinaryOperation op );

partial_sum() , [first,last), , , . , {0,1,1,2,3,5,8} {0,1,2,4,7,12,20}, , , (0,1,1) (2), 4.

, . , {1,2,3,4} - times<int>. {1,2,6,24}. OutputIterator .

partial_sum()  . <numeric>.

#include <numeric>

#include <vector>

#include <iostream.h>

 

/*

* :

: 1 3 4 5 7 8 9

:

1 4 8 13 20 28 37

times<int>():

1 3 12 60 420 3360 30240

*/

int main()

{

const int ia_size = 7;

int ia[ ia_size ] = { 1, 3, 4, 5, 7, 8, 9 };

int ia_res[ ia_size ];

 

ostream_iterator< int > outfile( cout, " " );

vector< int, allocator > vec( ia, ia+ia_size );

vector< int, allocator > vec_res( vec.size() );

 

cout << ": ";

copy( ia, ia+ia_size, outfile ); cout << endl;

 

cout << " :\n";

partial_sum( ia, ia+ia_size, ia_res );

copy( ia_res, ia_res+ia_size, outfile ); cout << endl;

 

cout << " times<int>():\n";

partial_sum( vec.begin(), vec.end(), vec_res.begin(),

times<int>() );

 

copy( vec_res.begin(), vec_res.end(), outfile );

cout << endl;

}




- -  - -  - -