我有一个不规则列表,其中的数据如下所示:
[Number] [Number]
[Number] [Number] [Number]
[Number] [Number] [Number]
[Number] [Number] [Number]
[Number] [Number] [Number]
[...]
请注意,有些行有 2 个数字,有些有 3 个数字。 目前我的输入代码如下所示
inputFile >> a >> b >> c;
但是,我想让它忽略只有 2 个数字的行,有没有简单的解决方法? (最好不使用字符串操作和转换)
谢谢
最佳答案
使用getline然后分别解析每一行:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string line;
while(std::getline(std::cin, line))
{
std::stringstream linestream(line);
int a;
int b;
int c;
if (linestream >> a >> b >> c)
{
// Three values have been read from the line
}
}
}
关于c++ - ifstream 的 operator>> 来检测行尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6850246/