#include<iostream>
#include<numeric>
#include<vector>
using namespace std;
//算术生成算法属于小型算法,包含头文件numeric
//accumulate 计算容器区间内元素累加总和
void test01()
{
vector<int>v1 = { 0,1,2,3,4,5 };
//accumulate(iterator beg, iterator end , 起始累加值);
int num = accumulate(v1.begin(), v1.end(), 0);
//相当于0+区间内的所有值
cout << num << endl;
}
//fill 向容器区间中填充元素
void test02()
{
vector<int>v;
v.resize(5);
//将区间内元素替换成1
fill(v.begin(), v.end(), 1);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
C++:STL-常用算术生成算法
最新推荐文章于 2022-07-02 21:57:21 发布