1、头文件 #include<numeric>
2、accumulate
2.1 计算容器元素累计总和
2.2 第三个参数是起始累加的值
3、fill
3.1 填充算法
4、测试程序
#include"pch.h"
#include<iostream>
#include<vector>
#include<numeric>//accumulate
#include<iterator>
using namespace std;
//accumulate 计算容器元素累计总和
void test01()
{
vector<int>v1;
for (int i = 1; i <=100; i++)
{
v1.push_back(i);
}
//第三个参数是起始累加的值
int sum = accumulate(v1.begin(), v1.end(),0);
cout << "sum= " << sum << endl;
}
//fill算法
void test02()
{
vector<int>v2;
v2.resize(10);
cout << "befor fill: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
fill(v2.begin(), v2.end(), 500);
cout << "after fill: ";
copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
int main()
{
//test01();
test02();
}