How to fill a vector with non-trivial initial values?

后端 未结 6 1480
一向
一向 2021-02-05 12:38

I know how to fill an std::vector with non-trivial initial values, e.g. sequence numbers:

void IndexArray( unsigned int length, std::vector&a         


        
6条回答
  •  后悔当初
    2021-02-05 13:25

    I usually go with std::generate plus a simple generator:

    template 
    struct gen {
        T x;
        gen(T seed) : x(seed) { }
    
        T operator ()() { return x++; }
    };
    
    generate(a.begin(), a.end(), gen(0));
    

提交回复
热议问题