我正在尝试实现一种算法,我想将用户输入的元素输入到 2D vector 中,这样我就有了这样的元素:
reference 1:
1 2 3
3 2 1
1 2 3
所以我想知道如何将元素 push_back 成二维 vector
我的问题是:
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!!
}
}
最佳答案
这是解决方案
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++ - 用户将元素输入二维 vector C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36877567/