How do I print a 2D/ vector of vectors?

For example, if I was using a 2dimensional array, I would print it out like this:

1
2
3
4
5
6
  for(int i = 0; i<row; i++) {
for(int j = 0; j<column; j++) {
cout << '(' << matrix[i][j] << ")";
}
cout << "\n";
}


What is the syntax for a printing that if it is in vector form like this:
vector<vector<int>> grid

1
2
3
4
5
for ( const std::vector<int> &v : grid )
{
   for ( int x : v ) std::cout << x << ' ';
   std::cout << std::endl;
}
Last edited on
Or


1
2
3
4
5
6
7
8
for ( std::vector<std::vector<int>>::size_type i = 0; i < grid.size(); i++ )
{
   for ( std::vector<int>::size_type j = 0; j < grid[i].size(); j++ )
   {
      std::cout << grid[i][j] << ' ';
   }
   std::cout << std::endl;
}
Thank you, I'm new to 2d/ vector of vectors, so was getting myself confused
Topic archived. No new replies allowed.