traverse 2d vector c++ code example
Example 1: how to make a 2d vector in c++
// Create a vector containing n
//vectors of size m, all u=initialized with 0
vector<vector<int> > vec( n , vector<int> (m, 0));
Example 2: size of a matrix using vector c++
// finding size of a square matrix
myVector[0].size();
Example 3: how to iterate over 2d vector c++
#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;
}