我想把这个记事本里的数据读到二维数组里去,但是二维数组的大小应该是根据这个文件的大小而定的,因为这个文件里的数据行和列可以更改。但是数组的行和列数不建议用未知数,而且我也不知道读取文件怎么可以知道行列数。
把下面程序cin
替换成文件流即可
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{
vector<vector<int>> data;
string line;
while (getline(cin, line))
{
istringstream ss(line);
vector<int> row;
int x;
while (ss >> x)
row.push_back(x);
data.push_back(move(row));
}
auto rows = data.size();
auto cols = data[0].size();
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
cout << data[i][j] << ' ';
cout << '\n';
}
return 0;
}