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

  • 代码如下:
#include 
#include 

using namespace std;

int main(){
    int x = 0;
    vector> vec;
    vector 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;
}

你可能感兴趣的:(C++)