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));
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));
size of a matrix using vector c++
// finding size of a square matrix
myVector[0].size();
how to get size of 2d vector in c++
myVector[
Vector[0, 4, 2, 5],
Vector[1, 4, 2]
];
/*When you call for myVector[1].size() it would return 3 and [0] would return 4.
For the amount of rows (int vectors) in the 2d vector, you can just use myVector.size()
You can run this to see it in actions*/
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);
}
}
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;
}
}
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.push_back(temp);// I don't know how to push_back here!!
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us