是否有任何标准的非读取方式来检查输入流中是否有任何待处理的字符?我总是可以尝试像
char buffer[256];
std::cin.get(buffer, 256, '\n');
if (!std::cin.fail()) {
// use buffer
}
但它谴责我使用C字符串等玩ios标志。难道没有更简单,更清洁的方法吗?像这样:if (std::cin.has_data()) {
std::string data;
std::getline(std::cin, data);
// do my stuff
}
最佳答案
函数peek是解决方案。
正如cppreference所说:
If good() == true, returns the next character as obtained by rdbuf()->sgetc()
Otherwise, returns Traits::eof().
关于c++ - 简单的方法来检查istream中是否有待处理的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64260878/