Answers for "passing 2d vector to function"

C++
0

passing 2d vector to function

void printFunc(vector < vector<int> > vec)
{
    for(int i=0; i<vec.size(); i++) 
		for(int j=0; j<vec[i].size(); j++) 
  			cout<<vec[i][j]<<" ";
        cout<<endl;
}

int main() 
{
    int rows = 2;
    int cols = 2;
    int val = 1;
	/*creates 2d vector “v[rows][cols]”
    and initializes all elements to “val = 1”*/
    vector< vector<int> > v(rows, vector<int> (cols, val));  
	printFunc(v);
}
Posted by: Guest on May-20-2021

Browse Popular Code Answers by Language