gpt4 book ai didi

c++ - 多次提取和插入时奇怪的字符串流行为。

转载 作者:行者123 更新时间:2023-11-30 01:13:21 24 4
gpt4 key购买 nike

谁能帮我理解这个 stringstream 行为?

stringstream temp;
temp << "342 1 ";
int a;
while (temp >> a) {
cout << a << endl;
}
temp << "56" << " ";
temp >> a;
cout << a << endl;

哪些输出:

342
1
1

我希望它能输出

342
1
56

这是在 visual studios 2015 中编译的。

最佳答案

读取值 1 后,下一个 while 将到达文件末尾并将流置于错误状态。任何进一步的读取都将失败,并保持 a 不变。

您可以通过调用 temp.clear() 清除错误状态。

关于c++ - 多次提取和插入时奇怪的字符串流行为。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32422956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com