[英]C++ Loading information from text file into a 2D Array
我是一个非常新手的程序员,遇到了一些问题。 我需要使用存储在文本文件中的数据加载2D数组。 文本文件的内容如下(两个数字,然后是行的末尾。即1 1949,然后是下一行):
1949年1月1日
1972年2月2日
1983年3月3日
1959年4月4日
1987年5月5日
1991年6月6日
1995年7月
1991年8月8日
1957年9月9日
1980年10月10日
1995年11月11日
1995年12月12日
数组的格式应相同。 当然不是在这里寻找答案,而是朝着正确的方向前进。 我一直没有用。 谢谢。
在这里,我使用pair<int, int>
来存储每一行; 如果您具有C ++ 11,则可以改用array<int, 2>
。 剩下的就是样板,读取和分割线。
#include <cassert>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
assert(argc == 2);
ifstream input(argv[1]);
assert(input);
vector<pair<int, int> > data;
for (string line; getline(input, line); )
{
istringstream stream(line);
data.resize(data.size() + 1);
stream >> data.back().first >> data.back().second;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.