example_13_9.cpp

来自「data+structures+using+c的源码」· C++ 代码 · 共 43 行

CPP
43
字号
//STL Functions generate and generate_n

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

using namespace std;

int nextNum();

int main()
{
   vector<int> vecList(8);                             //Line 1

   ostream_iterator<int> screen(cout, " ");            //Line 2

   generate(vecList.begin(), vecList.end(), nextNum);  //Line 3

   cout<<"Line 4: vecList after filling with "
       <<"numbers: ";                                  //Line 4

   copy(vecList.begin(), vecList.end(), screen);       //Line 5
   cout<<endl;                                         //Line 6

   generate_n(vecList.begin(), 3, nextNum);            //Line 7

   cout<<"Line 8: vecList, after filling the first three "
       <<"elements "<<endl
       <<"        with the next number: ";             //Line 8
   copy(vecList.begin(), vecList.end(), screen);       //Line 9
   cout<<endl;                                         //Line 10

   return 0;
}

int nextNum()
{
    static int n = 1;

    return n++;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?