For the outer std::vector you can't use "reserve()" because the std::vector can't know how to align the memory. Realize that the inner std::vector can vary in size per outer element!
Here are a few solutions.
1) use resize():
Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int Nv = 5;
int Nevx = 2;
int MAX = 99999;
vector<vector<int> > A;
A.resize(Nv);
for(unsigned i=0; i<Nv; ++i)
{
for (int j=0; j<Nv; ++j)
{
A[j].reserve(Nevx);
A[i][j] = MAX;
cout<<"A["<<i<< "]["<< j<< "]= "<<A[i][j]<<endl;
}
}
return 0;
}
2) Resize at creation
Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int Nv = 5;
int Nevx = 2;
int MAX = 99999;
vector<vector<int> > A(Nv);
for(unsigned i=0; i<Nv; ++i)
{
for (int j=0; j<Nv; ++j)
{
A[j].reserve(Nevx);
A[i][j] = MAX;
cout<<"A["<<i<< "]["<< j<< "]= "<<A[i][j]<<endl;
}
}
return 0;
}
TIP 1:
Besides, if you are absolutely sure that your 2D vector is going to hold numbers only, then consider learning the ways of std::valarray as the inner of std::vector.
TIP 2:
Try to use "unsigned" integers for vector indices by heart. If your vector contains more elements then the signed integer can handle then weird things will happen. A vector guarantees access times of O(n) so it is very feasable to assume that vectors will have lots of elements. Be prepared.