//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
vector<int> myvector;
for(int j = 0; j<CC; j++)
{
int tempVal = 0;
cout<<"Enter the number for Matrix 1";
cin>>tempVal;
myvector.push_back(tempVal);
}
matrix.push_back(myvector);
}
It is bad for your performance. Can someone explain why? And what should I do instead if I want to extract data from a file with an unknown size? (obviously the code I posted cannot extract data but it could be alterated to do so...)
1) it is less of perfomance hit in C++11 because of move semantic.
2) If you move vector (myvector) instead of copying it, it will be even faster.
I see, you do get your dimensions before actual reading, so you can reserve space beforehand to avoid reallocation at all.
Or you can resize them and work like with usual 2D arrays.
When you do push_back() on a vector, there is a chance that new size will exceed capacity.
It will have to reallocate its storage to be bigger and move all previously stored elements. Before (in C++03) it was done by copying each element and then deleting others. It is fine for primitive types, but for complex ones which handles their own storage it is slow: for example for vectors of vectors each vector had do allocate new storage, copy all data and deallocate old storage, calling destructor of all stored elements.
In C++11 move was introduced. Now when reallocating data, it will move elements instead of copy. For example for vectors it is as easy as copy four pointers! No matter how many elements it contains! No excess copy, no excess reallocations, no constantly created and destroyed objects. So in C++11 vector of vectors has way less overhead than it had in C++03.