Can I initialize an STL vector with 10 of the same integer in an initializer list?

后端 未结 6 1721
遇见更好的自我
遇见更好的自我 2021-01-31 07:54

Can I initialize an STL vector with 10 of the same integer in an initializer list? My attempts so far have failed me.

6条回答
  •  醉话见心
    2021-01-31 08:02

    You can do that with std::vector constructor:

    vector(size_type count, 
                     const T& value,
                     const Allocator& alloc = Allocator());
    

    Which takes count and value to be repeated.

    If you want to use initializer lists you can write:

    const int x = 5;
    std::vector vec {x, x, x, x, x, x, x, x, x, x};
    

提交回复
热议问题