Answers for "define 2d vector c++"

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
1

how to take input in 2d vector in c++

std::vector<vector<int>> d;
//std::vector<int> d;
cout<<"Enter the N number of ship and port:"<<endl;
cin>>in;
cout<<"Enter preference etc..:n";
for(i=0; i<in; i++){ 
cout<<"ship"<<i+1<<":"<<' ';
    for(j=0; j<in; j++){
    cin>>temp;
    d[i].push_back(temp); 
    }
}
Posted by: Guest on July-14-2020
1

c++ 2D vectors

int main() 
{ 
    int row = 5; // size of row 
    int colom[] = { 5, 3, 4, 2, 1 }; 
  
    vector<vector<int> > vec(row);  // Create a vector of vector with size equal to row. 
  	for (int i = 0; i < row; i++) { 
  		int col;  
        col = colom[i]; 
  		vec[i] = vector<int>(col); //Assigning the coloumn size of vector
        for (int j = 0; j < col; j++) 
            vec[i][j] = j + 1; 
    } 
  
    for (int i = 0; i < row; i++) { 
        for (int j = 0; j < vec[i].size(); j++) 
            cout << vec[i][j] << " "; 
        cout << endl; 
    } 
}
Posted by: Guest on July-27-2020

Browse Popular Code Answers by Language