何时何地清除c++中的输入流
When and where to clear input stream in c++
以下是加速c++中的一个示例:
// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}
in.clear()
行应该清除以前输入可能产生的错误,但为什么在我们开始输入之前,即while (in >> x)
行之前,它没有被清除?事实上,两次清除输入流(在输入hw之前和之后)不是更安全吗?
您在到达流的末尾后清除标志,因为您打算到达流的末端。由另一个函数引发的标志可能不是有意的,可能需要处理。
相关文章:
- 如何忽略输入流中的某些字符?
- C++输入流.与 cin unget() 函数有点混淆
- 从输入流中读取行,并能够跳过块
- 使用 Visual Studio 代码读取 C++ 中的输入流时出错
- 处理输入流运算符>>中的无效输入
- 树莓派上的端口音频,找不到输入流的设备
- 如何将C++输入流分隔符包含在结果令牌中
- 如果两个输入流相等,我如何构造主()到输出是
- 使用非成员函数从输入流中提取类对象
- CIN是否将输入流中的无效字符取用
- 动态内存分配和输入(流)运算符C++
- 计算输入流中值的出现次数
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 为什么输入流无法识别 Ctrl D 并给出无限循环
- java 输入流,字节大,然后是 127
- 检查是否为输入流设置了Skipws/noskipws标志
- 从Portaudio到WebRTC :: AudioProcessing馈送输入流
- std::setfill 和 std::setw 用于输入流?
- 将狭窄的输入流重新解释为宽(WCHAR_T)流
- 提升输入流错误.如何将标准:矢量的动态矢量另存为 XML 文件