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

Xavier picture Xavier · Apr 20, 2012 · Viewed 70.7k times · Source

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

Answer

Ed S. picture Ed S. · Apr 20, 2012

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

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