输入:
4
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

方法一:

#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
    string s;
    vector<vector<int>> nums;
    int cnt = 0;
    cin >> cnt;
    for (int i = 0; i <= cnt; i++)
    {
        getline(cin, s);
        stringstream ss(s);
        string str;
        vector<int> temp;
        while (ss >> str)
        {
            temp.push_back(stoi(str));
        }
        nums.push_back(temp);
    }
    for (vector<int> num : nums)
    {
        for (int t : num)
        {
            cout << t << " ";
        }
        cout << endl;
    }
    return 0;
}

方法二:

#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
    int m;
    vector<vector<int>> nums;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        vector<int> temp;
        for (int j = 0; j < m; j++)
        {
            int a;
            cin >> a;
            temp.push_back(a);
        }
        nums.push_back(temp);
    }
    for (vector<int> num : nums)
    {
        for (int t : num)
        {
            cout << t << " ";
        }
        cout << endl;
    }
    return 0;
}

1、https://www.cnblogs.com/emerson027/articles/9319135.html
2、https://blog.csdn.net/herbertpotter/article/details/78075843

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐