To create a std::vector
of std::vectors
of a given size x
, use
std::vector<std::vector<int>> foo(x, std::vector<int>(x));
To print it you can use loops:
for (std::size_t row{}; row < x; ++row) {
for (std::size_t col{}; col < x; ++col)
std::cout foo[row][col] << ' ';
std::cout.put('\n');
}
A more efficient way of having a matrix is to use a std::vector<int>
of the total size of rows and columns and calculate the index accordingly:
std::vector<int> foo(x * x);
// access column `c` at row `r` like
foo[r * x + c];
Print it:
for(std::size_t i{}; i < x; ++i, std::cout.put('\n'))
std::copy(foo.begin() + i * x, foo.begin() + (i+1) * x,
std::ostream_iterator<int>(std::cout, " "));