dimanche 18 août 2019

How to reserve a multi-dimensional Vector without increasing the vector size?

I have data which is N by 4 which I push back data as follows.

vector<vector<int>> a;
for(some loop){
   ...
   a.push_back(vector<int>(4){val1,val2,val3,val4});
}

N would be less than 13000. In order to po prevent unnecessary reallocation I would like to preserve 13000 by 4 space in advance.

After reading multiple related posts on this topic(eg this) I know the followings will do the work. But I would like to do it with reserve or any similar function if there are any, to be able to use push_back.

vector<vector<int>> a(13000,vector<int>(4);

or

vector<vector<int>> a;
a.resize(13000,vector<int>(4));

How can I just preserve memory without increasing the vector size?

Aucun commentaire:

Enregistrer un commentaire