如何在 c++11 中检测来自 istream 的空白行

How to detect blank lines from istream in c++11?

本文关键字:istream 空白 检测 c++11      更新时间:2023-10-16

如何在 c++11 中检测来自 istream 的空白行?

有没有一个简单的方法,或者我将不得不做类似的事情删除空格和制表符,然后查看生成的字符串是否是空的?

std::string s;
if (!std::getline(in, s)) {
    // There wasn't another line.
}
auto pred = [](unsigned char const c) { return std::isblank(c); };
if (std::all_of(s.begin(), s.end(), pred)) {
    // This line was empty.
}