passing 2d vector to function

Solutions on MaxInterview for passing 2d vector to function by the best coders in the world

showing results for - "passing 2d vector to function"
Federica
04 Jul 2020
1void printFunc(vector < vector<int> > vec)
2{
3    for(int i=0; i<vec.size(); i++) 
4		for(int j=0; j<vec[i].size(); j++) 
5  			cout<<vec[i][j]<<" ";
6        cout<<endl;
7}
8
9int main() 
10{
11    int rows = 2;
12    int cols = 2;
13    int val = 1;
14	/*creates 2d vector “v[rows][cols]”
15    and initializes all elements to “val = 1”*/
16    vector< vector<int> > v(rows, vector<int> (cols, val));  
17	printFunc(v);
18}
19