We\'re trying to use a 2D vector because we want a 2D array that will grow dynamically.
We tried this: In the class declaration:
vector
You can access the element in [][] manner by derefrencing.
Vector<vector<double>> *table ;
table = new vector<vector<double>> ( n, vector<double>( m, 0.0)) ;
cout << (*table)[i][j] ;
Most of the times, this works perfectly well.
Make sure your vectors are large enough to store your elements. If a vector t
has size N
, the last element you can access is t[N-1]
.
t = vector<vector<double> > (10, vector<double>(10));
t[50] = vector<double>(5); // This is wrong! Vector size is 10, you access 50th.
t[50][10] = 10; // Wrong again! Vector size 5, you access 10th.
If you have Boost installed try using Boost Multi-array.
You'll need to resize the tables before you access elements.
vector<vector<double> > table;
table.resize(10);
for (int i = 0; i < 10; ++i)
table[i].resize(20);