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

后端 未结 6 1727
遇见更好的自我
遇见更好的自我 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:08

    Use the appropriate constructor, which takes a size and a default value.

    int number_of_elements = 10;
    int default_value = 1;
    std::vector vec(number_of_elements, default_value);
    

提交回复
热议问题