print out a 2d array c++ code example

Example 1: print 2d vector c++

for(int i=0; i

Example 2: print a 2d vector in c++

// A recursive function able to print a vector
// of an arbitrary amount of dimensions.
template
static void show(T vec)
{
  std::cout << vec;
}


template
static void show(std::vector vec)
{
  int size = vec.size();
  if (size <= 0) {
    std::cout << "invalid vector";
    return;
  }
  std::cout << '{';
  for (int l = 0; l < size - 1; l++) {
    show(vec[l]);
    std::cout << ',';
  }
  show(vec[size - 1]);
  std::cout << '}';
}

Example 3: how to print a 2d array in c++

for (int i = 0; i < m; i++) 
{ 
   for (int j = 0; j < n; j++) 
   { 
      cout << arr[i][j] << " "; 
   } 
     
   // Newline for new row 
   cout << endl; 
}

Tags:

Misc Example