++


accumulate()


template < class InputIterator, class Type >

Type accumulate(

InputIterator first, InputIterator last,

Type init );

 

template < class InputIterator, class Type,

class BinaryOperation >

Type accumulate(

InputIterator first, InputIterator last,

Type init, BinaryOperation op );

accumulate() , [first,last), , init. , {1,1,2,3,5,8} 0, 20. . accumulate() - times<int> 1, 240. accumulate()  ; <numeric>.

#include <numeric>

#include <list>

#include <functional>

#include <iostream.h>

 

/*

* :

* accumulate()

* {1,2,3,4}

* : 10

* - plus<int>: 10

*/

 

int main()

{

int ia[] = { 1, 2, 3, 4 };

list<int,allocator> ilist( ia, ia+4 );

int ia_result = accumulate(&ia[0], &ia[4], 0);

int ilist_res = accumulate(

ilist.begin(), ilist.end(), 0, plus<int>() );

 

cout << "accumulate()\n\t"

<< " {1,2,3,4}\n\t"

<< " : "

<< ia_result << "\n\t"

<< " - plus<int>: "

<< ilist_res

<< endl;

return 0;

}




- -  - -  - -