c++ - 用户将元素输入二维 vector C++

标签 c++ vector user-input

我正在尝试实现一种算法,我想将用户输入的元素输入到 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/

相关文章:

c++ - 为什么在这种情况下不隐式生成 operator= 和复制构造函数?

c++ - 如何从 vector <char>中提取不同数据类型的子 vector ?

c++ - STL vector 、迭代器和插入 (C++)

python - 用户输入值用作十进制值python

php - 使 Web 表单输入在各种情况下都安全的正确方法是什么?

c++ - 在 C++ 结构中,构造函数中的默认值和默认参数有什么区别?

c++ - 在离开模式下唤醒 Windows

c++ - 在 C++ 中读取制表符分隔的文件

r - 顺序删除向量元素

java - 将字符串数组转换为整数数组