accumulate函数用法

unction template
<numeric>

std::accumulate

sum (1)
template <class InputIterator, class T>
   T accumulate (InputIterator first, InputIterator last, T init);
custom (2)
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) toinit.

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

The behavior of this function template is equivalent to:

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. template <class InputIterator, class T>  
  2.    T accumulate (InputIterator first, InputIterator last, T init)  
  3. {  
  4.   while (first!=last) {  
  5.     init = init + *first;  // or: init=binary_op(init,*first) for the binary_op version  
  6.     ++first;  
  7.   }  
  8.   return init;  
  9. }  



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 firstandlast, including the element pointed by first but not the element pointed bylast.
init
Initial value for the accumulator.
binary_op
Binary operation taking an element of type T as first argument and an element in the range as second, and which returns a value that can be assigned to typeT.
This can either be a function pointer or a function object.
The operation shall not modify the elements passed as its arguments.

 

Return value

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

Example

 

[cpp] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. // accumulate example  
  2. #include <iostream>     // std::cout  
  3. #include <functional>   // std::minus  
  4. #include <numeric>      // std::accumulate  
  5.   
  6. int myfunction (int x, int y) {return x+2*y;}  
  7. struct myclass {  
  8.     int operator()(int x, int y) {return x+3*y;}  
  9. } myobject;  
  10.   
  11. int main () {  
  12.   int init = 100;  
  13.   int numbers[] = {10,20,30};  
  14.   
  15.   std::cout << "using default accumulate: ";  
  16.   std::cout << std::accumulate(numbers,numbers+3,init);  
  17.   std::cout << '\n';  
  18.   
  19.   std::cout << "using functional's minus: ";  
  20.   std::cout << std::accumulate (numbers, numbers+3, init, std::minus<int>());  
  21.   std::cout << '\n';  
  22.   
  23.   std::cout << "using custom function: ";  
  24.   std::cout << std::accumulate (numbers, numbers+3, init, myfunction);  
  25.   std::cout << '\n';  
  26.   
  27.   std::cout << "using custom object: ";  
  28.   std::cout << std::accumulate (numbers, numbers+3, init, myobject);  
  29.   std::cout << '\n';  
  30.   
  31.   return 0;  
  32. }  



 

Output:


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

 

Complexity

Linear in the distance betweenfirst and last.

Data races

The elements in the range [first,last) are accessed (each element is accessed exactly once).

Exceptions

Throws if any of binary_op, the assignments or an operation on an iterator throws.

Note that invalid arguments cause undefined behavior.

 

http://www.cplusplus.com/reference/numeric/accumulate/?kw=accumulate

posted @ 2017-03-14 14:57  mxp_neu  阅读(1330)  评论(0)    收藏  举报