我正在尝试编写一个代码,在读取txt文件时会跳过最后一个单词。我不知道如何输出行而不包含行,包括最后一行和前面的空格。任何帮助将不胜感激,我是c ++的新手。
答案 0 :(得分:2)
这是模拟输入的一种相当简单的方法:
for (std::string line : {"hello im joe", "abc def", "123", "1 2 3 4 5"}) {
auto pos = line.find_last_of(' '); //find last space
if (pos == std::string::npos) {
continue; //don't print anything if not found
}
//print substring from beginning to space position
std::cout << line.substr(0, pos) << '\n';
}