<分区>
我正在阅读 C++ 入门书,并对以下代码示例感到好奇:
string buf;
while (cin >> buf && !buf.empty()) {
if (buf[0] != '_')
continue; // get another input
//the input starts with an underscore; process buf . . .
}
循环应该忽略不以下划线开头的单词并处理以下划线开头的单词。
我的问题是关于条件
(cin >> buf && !buf.empty())
我会说当 (cin >> buf) 为真时,条件 (!buf.empty()) 始终为真,所以我不明白添加它的意义。 有没有第二个条件不是多余的情况?
之前有一个关于堆栈溢出的问题,关于类似的构造 ( Is it possible to read an empty string from cin and still get true from cin.good()? ),其答案是否定的(第二个条件是多余的)。
如果这是正确的,为什么会出现在书上?这只是一个错误? 或者有一些特殊情况,双重条件是否有意义?