c++ vector 2d 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 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); 
    }
}

Tags:

Cpp Example