算术生成算法操作指南

#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <numeric>
void myprint4(int val)
{
    cout << val << " ";
}
//一、accumulate
/*
accumulate(iterator beg, iterator end, value);
计算容器元素累计总和
beg开始迭代器
end结束迭代器
value起始值
*/
void t00001()
{
    vector<int> v1;
    for (int i = 0; i <= 100; i++)
    {
        v1.push_back(i);
    }
    int sum = accumulate(v1.begin(), v1.end(), 110);//110相当于基数,在110的基础上加5050
    cout << "sum:" << sum;
}

//二、fill
/*
fill(iterator beg, iterator end, value);
向容器中填充元素
beg开始迭代器
end结束迭代器
value填充的值
*/
void t00002()
{
    vector<int> v1;
    v1.resize(10);
    for_each(v1.begin(), v1.end(), myprint4);
    cout << endl;
    fill(v1.begin(), v1.end(), 100);
    for_each(v1.begin(), v1.end(), myprint4);
    cout << endl;
}

int main()
{
    t00002();
    return 0;
}

 

posted @ 2021-08-28 14:42  花与不易  阅读(95)  评论(0)    收藏  举报