accumulate函数
来源:互联网 发布:软件设计师考试真题 编辑:程序博客网 时间:2024/05/17 20:29
function template
<numeric>
std::accumulate
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:
123456789
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 ++first; } 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 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 type T.
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
1234567891011121314151617181920212223242526272829303132
// accumulate example#include <iostream> // std::cout#include <functional> // std::minus#include <numeric> // std::accumulateint 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}; std::cout << "using default accumulate: "; std::cout << std::accumulate(numbers,numbers+3,init); std::cout << '\n'; std::cout << "using functional's minus: "; std::cout << std::accumulate (numbers, numbers+3, init, std::minus<int>()); std::cout << '\n'; std::cout << "using custom function: "; std::cout << std::accumulate (numbers, numbers+3, init, myfunction); std::cout << '\n'; std::cout << "using custom object: "; std::cout << std::accumulate (numbers, numbers+3, init, myobject); std::cout << '\n'; return 0;}
Output:
using default accumulate: 160using functional's minus: 40using custom function: 220using custom object: 280
Complexity
Linear in the distance between first 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.
0 0
- accumulate函数
- accumulate everyday:grep()函数
- accumulate函数用法
- accumulate(求和函数)
- C++ 11.2 accumulate、find_first_of 函数
- C++新手之accumulate函数
- accumulate算法函数求和示例
- accumulate
- accumulate
- accumulate
- STL算法 count_if函数 accumulate函数
- STL中accumulate函数的使用
- STL中accumulate函数的使用
- C++ STL算法之accumulate函数
- STL算法之accumulate函数学习
- C++ STL算法之accumulate函数
- 笔记:STL中的函数:accumulate(求和)
- opencv2 accumulate()函数的使用,用于求几幅图像的平均值
- OC 类 和 对象
- Modify the /etc/apt/sources.list for Ubuntu
- C与C++中结构体的区别,C++中结构体和类的区别
- 蓝桥杯——2的幂次
- JAVA--接口
- accumulate函数
- C++基础——使用字符串作为函数模板的实参
- AxureRP7.0 MAC
- 栈的实现与应用
- 欢迎使用CSDN-markdown编辑器
- 获取SDCard存储大小
- ios多线程编程
- 利用XStream将List<String>转为xml后指定String的别名
- .NET WinForm中 一个 跨线程访问控件的示例(网络)