输入:
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