C++从键盘输入二维数组

  • Post author:
  • Post category:其他


  • 代码如下:
#include <iostream>
#include <vector>

using namespace std;

int main(){
    int x = 0;
    vector<vector<int>> vec;
    vector<int> v;
    while(cin >> x){
        v.push_back(x);
        if(cin.get() == '\n'){
            vec.push_back(v);
            v.clear();
        }


        if(cin.peek() == '\n'){
            vec.push_back(v);
            break;
        }
    }
    
    cout << "row:" << vec.size() << endl;
    cout << "col:" << v.size() << endl;
    cout << "验证输出\n";
    for(int i = 0; i < vec.size(); i++){
        for(int j = 0; j < v.size(); j++){
            cout << vec[i][j] << " ";
        }
    }
    return 0;
}



版权声明:本文为cdlwhm1217096231原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。