There are may ways to create a multidimensional vectors but I use this method which utilizes struct,
Here is an example for 2X2 table
#include<iostream>
#include<vector>
struct table
{
std::vector<int> column;
};
int main()
{
std::vector<table> myvec;
// values of the column for row1
table t1;
t1.column = {1,2};
// Push the first row
myvec.push_back(t1);
// values of the column for row2
table t2;
t2.column = { 3, 4};
// push the second row
myvec.push_back(t2);
// now let us see whether we've got a 2x2 table
for(auto row : myvec)
{
auto values = row.column;
for(auto value : values) std::cout<< value << " ";
std::cout<<"\n";
}
// Now we will try to get a particular value from the column index of a particular row
table row = myvec[1]; // 2nd row
std::cout<<"The value present at 2nd row and 1st column is: "<<row.column[0] <<"\n";
}
which gives me,
1 2
3 4
The value present at 2nd row and 1st column is: 3
You can easily change it to different dimensions.
Note: I have posted this answer to my knowledge if it is wrong somebody correct me. Thank you
new
. As tostd::list
, what exactly do you mean by a2d list
? a vector ofstd::list
?delete []
is not made at the proper time. There are thousands of questions on StackOverflow where the poster does the first code, and wonders why Valgrind gives errors, why the destructor crashes, etc. And to a list of list, why not simply compile it and see if it worksstd::list<std::list<int>>