In this article, we will discuss different ways to print the contents of a two dimensional vector in C++.

**Table of Contents**

- Print two dimensional vector using range based for loops
- C++: Print 2D vector by overloading << operator & using indexing
- Print 2D Vector / Matrix in one line using lambda inside lambda

In C++, two dimensional vector can be represented by creating a vector of vectors, where each of the nested vector in main vector represents a row and number of elements in nested vector represents the column values. Let’s see how to print this matrix look a like 2D vector in C++.

## Print two dimensional vector using range based for loops

In C++, a 2D vector or matrix can be constructed by creating a vector of vectors. To print this matrix, we can iterate over the parent vector using a range based for loop, and then for each of the nested vector, we can again use a range based for loop to print its contents. For example,

#include<iostream> #include<vector> using namespace std; /* Iterate over vector of vectors and for each of the nested vector print its contents */ template <typename T> void print_2d_vector(const vector< vector<T> > & matrix) { for(auto row_obj : matrix) { for (auto elem: row_obj) { cout<<elem<<", "; } cout<<endl; } cout<<endl; } int main() { // Initializing a 2D Vector i.e. vector of vectors vector<vector<int>> matrix = { {1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 }, {5, 6, 8, 1, 12 }, {1, 7, 2, 4, 18 }, }; // Print 2D vector / matrix print_2d_vector(matrix); return 0; }

**Output:**

**Advertisements**

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 8, 1, 12, 1, 7, 2, 4, 18,

We created a template function * print_2d_vector()*, which accepts a vector of vectors of any type and iterates over the 2d vector matrix row wise. It went through all of the nested vectors one by one and printed their contents in one row. Thus printed the whole matric one by one.

## C++: Print 2D vector by overloading << operator & using indexing

If you are using some old version of C++ (pre c+11 version) and your compiler does not support the range based for loop, then you can iterate over the vector and nested vectors using indexing. For example,

#include<iostream> #include<vector> using namespace std; /* Iterate over vector of vectors and for each of the nested vector print its contents */ template <typename T> std::ostream & operator << (std::ostream & os, const vector< vector<T> > & matrix) { for(int i = 0 ; i < matrix.size(); i++) { for (int j = 0; j < matrix.at(i).size(); j++) { os<<matrix[i][j]<<", "; } os<<endl; } os<<endl; return os; } int main() { // Initializing a 2D Vector i.e. vector of vectors vector<vector<int>> matrix = { {1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 }, {5, 6, 8, 1, 12 }, {1, 7, 2, 4, 18 }, }; // Print 2D vector / matrix cout<<matrix; return 0; }

**Output:**

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 8, 1, 12, 1, 7, 2, 4, 18,

We overloaded the << operator of vector of vectors. It iterates over the 2d vector / matrix row wise i.e.Â it went through all of the nested vectors one by one and printed their contents.

## Print 2D Vector / Matrix in one line using lambda inside lambda

We can use STL algorithm for_each() to iterate over the all the nested vectors inside the 2d vector and apply a lambda function on each of the nested vector. Inside the lambda function, we again called the for_each(), to apply another lambda function on each element to print its value.Â For example,

#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { // Initializing a 2D Vector i.e. vector of vectors vector<vector<int>> matrix = { {1, 2, 3, 4, 5 }, {6, 7, 8, 9, 10 }, {5, 6, 8, 1, 12 }, {1, 7, 2, 4, 18 }, }; // Print 2D vector / matrix for_each(matrix.begin(), matrix.end(), [](const auto & row ) { for_each(row.begin(), row.end(), [](const auto & elem){ cout<<elem<<", "; }); cout<<endl; }); cout<<endl; return 0; }

**Output:**

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 8, 1, 12, 1, 7, 2, 4, 18,

It iterates over the 2d vector / matrix row wise i.e. it went through all of the nested vectors one by one and printed their contents.Â Complexity wise this solution is similar to previous two solutions, but here we did all the stuff in a single line.

**Summary:**

We learned about different ways to print 1 two dimensional vector in c++.

**Advertisements**

Thanks for reading.