Answers for "pass 2d array to vector"

C++
1

how to create 2d array using vector in c++

vector<vector<int>> vec(N, vector<int> (M, INT_MAX));

Explanation::
vector<vector<int>> -- will take the formed container
N -- Think like row of 2d Matrix
vector<int> (M, INT_MAX) -- In each row, there is again a vector associated with it, 
that will formed 2d array.
Posted by: Guest on July-13-2021
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