use std::fill to populate vector with increasing numbers

后端 未结 15 904
深忆病人
深忆病人 2020-12-02 09:32

I would like to fill a vector using std::fill, but instead of one value, the vector should contain numbers in increasing order after.

15条回答
  •  温柔的废话
    2020-12-02 10:09

    If you really want to use std::fill and are confined to C++98 you can use something like the following,

    #include 
    #include 
    #include 
    #include 
    
    struct increasing {
        increasing(int start) : x(start) {}
        operator int () const { return x++; }
        mutable int x;
    };
    
    int main(int argc, char* argv[])
    {
        using namespace std;
    
        vector v(10);
        fill(v.begin(), v.end(), increasing(0));
        copy(v.begin(), v.end(), ostream_iterator(cout, " "));
        cout << endl;
        return 0;
    }
    

提交回复
热议问题