C++常用的算术生成算法
学习目标:
掌握常用的算术生成算法
注意:
算术生成算法属于小型算法,使用时包含的头文件为#include
算法简介:
accumulate //计算容器元素累计总和
fill //向容器中添加元素
accumulate
功能描述:
*
计算区间内容器元素累计总和
函数原型:
accumulate(iterator beg, iterator end, value);
//计算容器元素累计总和
//beg开始迭代器
//end结束迭代器
//value起始值
代码示例:
#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
void test01()
{
vector<int>v;
for (int i = 0; i <= 100; i++)
{
v.push_back(i);
}
//参数3 起始累加值
int total =accumulate(v.begin(), v.end(), 1000);
cout << "total=" << total << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结: accumulate使用时头文件注意是numeric,这个算法很实用