what is the most efficient way to initialize a vector? I don't want to initialize it in the constructor.
struct st{ std::vector<int> var_dynamic; int i; }; int main(){ for ( int k=1 ; k<10 ; k++) { struct st st1; st1.i=1; int i = 999; // some integer value st1.var_dynamic.reserve(100000000); // make room for 10 elements std::vector<int> vec(1000,0); for (int n=1; n<1000000;n++) { st1.var_dynamic.push_back(1); } } } I think this method couldn't be efficient.
std::vector<T>as soon as T is a Plain old Data (POD) (native C type or simple structs built from them)memset(&var[0], 0, sizeof(var[0]) * var.size())Notice that this method should fail on bool datatype. Another approach could be to use the fill function available in the algorithm header as following:std::fill(var.begin(),var.end(),0)