为什么我必须在这里清除 std::stringstream ? [英] Why do I have to clear std::stringstream here?

查看:31
本文介绍了为什么我必须在这里清除 std::stringstream ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简短的测试程序,看看我是否可以重复使用 stringstream 附加到一个字符串.

I wrote a short testprogram to see if I can append to a string using stringstream repeatedly.

在第一个版本中,我得到了 Output1,但我真的不明白为什么 s1 保持为空.我发现我必须做 ss.clear() 然后在 Output2 中得到预期的结果.任何人都可以解释为什么它在没有明确的情况下不起作用?我原以为,如果我反复输入数字并将它们取回字符串,我应该总是得到数字.我不确定是否附加了数字,但这不是本示例的重点.

In the first version I got Output1 and I don't really understand why s1 stays empty. I found out that I have to do ss.clear() and then get the expected result in Output2. Can anybody explain why it doesn't work without the clear? I would have expected that, If I repeatedly enter numbers and fetch them back into a string, I should get always the number. I was unsure if the number gets appended, but that is beside the point for this example.

这里:http://www.cplusplus.com/reference/sstream/stringstream/ 它说我可以使用任何操作,并且没有限制或要求重置字符串,我可以看到.我也不明白为什么之后我得到的输出中间没有 ss.clear().

Here: http://www.cplusplus.com/reference/sstream/stringstream/ it says I can use any operation, and there is no limitation or requirement to reset the string, I could see. I also don't understand why afterwards I get the output without the ss.clear() in between.

我也有点惊讶 s0 之后保持不变.因此,如果流已经有内容,它不会覆盖或重置字符串吗?

Also I'm a bit surprised that s0 stays the same afterwards. So a stream doesn't overwrite or reset the string if it already has content?

我在 cygwin 中使用 gcc 3.4.4.

I'm using gcc 3.4.4 with cygwin.

int main()
{
    std::string s0;
    std::string s1;
    int n = 1;
    std::stringstream ss;
    ss << n;
    ss >> s0;
    cout << "S0:" << s0 << endl;
    ss.clear();     <-- If I remove this, then s1 stays empty.
    n = 2;
    ss << n;
    ss >> s1;
    cout << "S1:" << s1 << endl;
    ss << n;
    ss >> s0;
    cout << "S0_2:" << s0 << endl;    <-- Why is s0 still 1?
 }

输出 1:

S0:1
S1:
S0_2:1

输出 2:

S0:1
S1:2
S0_2:1

推荐答案

读入s0后,流处于EOF状态.因此,除非清除 EOF 状态,否则下一次读取将失败.写入流不会为您清除读取状态.

After the read into s0, the stream is in the EOF state. So the next read fails unless the EOF state is cleared. Writing to the stream does not clear the read state for you.

编辑只是为了完成答案.该行为来自 ios_base::iostateeofbit 定义,它表示如果流位于输入序列的末尾,则流的状态将设置此位.

Edit just to complete the answer. The behavior comes from the definition of eofbit of ios_base::iostate which says the state of the stream will have this bit set if the stream is at the end of input sequence.

在你的程序的第一个版本中,由于在第一次读入 s0 后没有清除 EOF 状态,所以第二次读取和第三次读取都不会成功.因此,失败的第一次读取使 s1 为空,而失败的第二次读取使 s0 保持不变.

In the first version of your program, since the EOF state is not cleared after the first read into s0, neither the second read nor the third read will succeed. So, the failed first read leaves s1 empty, and the failed second read leaves s0 unchahnged.

在程序的第二个版本中,您在第一次读入 s0 后清除 ss,这允许第二次读入 s1成功.但是,在第二次读取之后,流再次处于EOF 状态,因此第三次读取失败.这使 s0 保持不变.

In the second version of your program, you clear ss after the first read into s0, which allows the second read into s1 to succeed. However, after the second read, the stream is in the EOF state again, so the third read fails. This leaves s0 unchanged.

这篇关于为什么我必须在这里清除 std::stringstream ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆