std::accumulate的作用是对一段区间内的数据进行求和
template <class InputIterator, class Type>
Type accumulate(
InputIterator first,
InputIterator last,
Type init //初始值
);
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main(){
vector<int> data = {1, 2, 3, 4, 5};
auto res = accumulate(data.begin(), data.end(), 0);
cout<<res<<endl;
return 0;
}
accumulate的最后一个参数是作为求和的初始值
运行程序输出:
15
accumulate还可以增加一个函数作为参数
template <class InputIterator, class Type, class BinaryOperation>
Type accumulate(
InputIterator first,
InputIterator last,
Type init,
BinaryOperation binary_op //应用于指定范围内每个元素的二元运算及其上一应用的结果
);
可以通过binary_op完成对字符串的指点格式连接:
#include <iostream>
#include <vector&