using namespace std;
vector<vector<T> > vec_collection(3);
vec_collection[0]=vector<T>(12);
vec_collection[1]=vector<T>(3);
vec_collection[2]=vector<T>(14);
Suppose I have an empty collection of 3 vectors and after initialization, I want the first, second, and third vector to be of specific size 12, 3, and 14 say. Is the above code snippet the right way to declare their sizes?
Your code in line of principle creates separated temporary
vectorinstances that replace the existing zero-size ones, which in theory means that for every of those lines you have a constructor call (create the temporary), a copy-constructor call (copy it over the existingvector) and a destructor call (destroy the temporary).Although I think that some optimizations could make it actually better than it sounds, it’s way easier and almost surely more efficient to simply do:
On the other hand, if you need such
vectors to be always of that fixed size, you should replace them with fixed-size data structures (e.g. astructcontaining three C-style arrays orstd::array), as in @sehe’s answer.