initialize a vector to zeros C++/C++11


I know in C++11 they added the feature to initialize a variable to zero as such

double number = {}; // number = 0
int data{};  // data = 0

Is there a similar way to initialize a std::vector of a fixed length to all zero's?

This question is tagged with c++ c++11

~ Asked on 2012-10-28 15:26:18

The Best Answer is


You don't need initialization lists for that:

std::vector<int> vector1(length, 0);
std::vector<double> vector2(length, 0.0);

~ Answered on 2012-10-28 15:28:11


Initializing a vector having struct, class or Union can be done this way

std::vector<SomeStruct> someStructVect(length);
memset(, 0, sizeof(SomeStruct)*length);

~ Answered on 2020-05-03 19:08:31

Most Viewed Questions: