STL-----------accumulate

 
 
如果要想真正学好STL 提供网站  http://www.cplusplus.com/reference/algorithm/count/
 
 
template <class InputIterator, class T>
   T accumulate ( InputIterator first, InputIterator last, T init );
template <class InputIterator, class T, class BinaryOperation>
   T accumulate ( InputIterator first, InputIterator last, T init,
                  BinaryOperation binary_op );
Accumulate values in range
Returns the result of accumulating all the values in the range [first,last) to init.

The default operation is to add the elements up, but a different operation can be specified as binary_op.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template <class InputIterator, class T>
   T accumulate ( InputIterator first, InputIterator last, T init )
{
  while ( first!=last )
    init = init + *first++;  // or: init=binary_op(init,*first++) for the binary_op version
  return init;
}


Parameters

first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
init
Initial value for the accumulator.
binary_op
Binary operation taking an object of type T as first argument and an element in the range as second. This can either be a pointer to a function or an object whose class overloads operator(). The operation is expected to return a value of type T.

Return value

The result of accumulating init and all the elements in the range [first,last).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// accumulate example
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;

int myfunction (int x, int y) {return x+2*y;}
struct myclass {
	int operator()(int x, int y) {return x+3*y;}
} myobject;

int main () {
  int init = 100;
  int numbers[] = {10,20,30};

  cout << "using default accumulate: ";
  cout << accumulate(numbers,numbers+3,init);
  cout << endl;

  cout << "using functional's minus: ";
  cout << accumulate (numbers, numbers+3, init, minus<int>() );
  cout << endl;

  cout << "using custom function: ";
  cout << accumulate (numbers, numbers+3, init, myfunction );
  cout << endl;

  cout << "using custom object: ";
  cout << accumulate (numbers, numbers+3, init, myobject );
  cout << endl;

  return 0;
}

 

#include <iostream>
#include <functional>
#include <numeric>
using namespace std;

int myfunction (int x, int y) {return x+2*y;}
class myclass {
public: int operator()(int x, int y) {return x+3*y;}
} ;

int main () {
  int init = 100;
  int numbers[] = {10,20,30};

  cout << "using default accumulate: ";
  cout << accumulate(numbers,numbers+3,init);
  cout << endl;

  cout << "using functional's minus: ";
  cout << accumulate (numbers, numbers+3, init, minus<int>() );
  cout << endl;

  cout << "using custom function: ";
  cout << accumulate (numbers, numbers+3, init, myfunction );
  cout << endl;

  cout << "using custom object: ";
  myclass myobject1;
  cout << accumulate (numbers, numbers+3, init, myobject1);
  cout << endl;

  return 0;
}

 

 



Output:

using default accumulate: 160
using functional's minus: 40
using custom function: 220
using custom object: 280

你可能感兴趣的:(STL-----------accumulate)