c++ std标准库 算法<algorithm> 赋值 fill() fill_n() generate() generate_n()

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int Fibonacci(void)
{
    static int r;
    static int f1 = 0;
    static int f2 = 1;
    r = f1 + f2;
    f1 = f2;
    f2 = r;
    return f1;
}

void main()
{
    vector<int> v1;
    vector<int> v2(5, 0), v3(6, 0);
    int dim[] = { 1,2,3,4,5,6,7,8,9 };

    cout << "【输出v1】" << endl;
    v1.assign(dim, dim + 9);
    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
    cout << endl;

    cout << "【fill 输出v1】" << endl;
    fill(v1.begin(), v1.begin() + 4, 9);
    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
    cout << endl;

    cout << "【fill_n 输出v1】" << endl;
    fill_n(v1.begin(), 5, 20);
    copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, ", "));
    cout << endl;

    cout << "【generate 输出v2】" << endl;
    generate(v2.begin(), v2.end(), rand);
    copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", "));
    cout << endl;
    
    cout << "【generate_n rand输出v2】" << endl;
    generate_n(v2.begin(), 3, rand);
    copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, ", "));
    cout << endl;
    
    cout << "【generate_n Fibonacci输出v3】" << endl;
    generate_n(v3.begin(), 6, Fibonacci);
    copy(v3.begin(), v3.end(), ostream_iterator<int>(cout, ", "));
    cout << endl;
}

c++ std标准库 算法<algorithm> 赋值 fill()  fill_n() generate() generate_n()

上一篇:canvas学习


下一篇:openpyl 设置单元格格式