字符串流到数组
你能告诉我,为什么这是错误的?
我有
mytype test[2];
stringsstream result;
int value;
for (int i=0; i<2; i++) {
result.str("");
(some calculating);
result<< value;
result>> test[i];
}
当我观察测试数组时 - 仅第一个 - test[0] - 具有正确的值 - 每个其他 test[1..x] 的值为 0 为什么它是错误的并且不起作用?在第一次循环运行时,字符串流将正确的值设置为数组,但后来只有 0?
谢谢
could you tell me, why is this wrong?
I have
mytype test[2];
stringsstream result;
int value;
for (int i=0; i<2; i++) {
result.str("");
(some calculating);
result<< value;
result>> test[i];
}
When I watch to test array - only first - test[0] - has correct value - every other test[1..x] has value 0
why its wrong and not working? in first run in cycle the stringstream set the correct value to array, but later there is only 0?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在使用
result.str("")
刷新字符串流之前,尝试使用result.clear()
处理字符串流。这将其设置为输出缓冲区后再次接受输入的状态。如果不清除,我会得到
test[0] == 0
和test[1] == -832551553 /*some random number*/
。通过clear
我得到了test[0] == 0
和test[1] == 1
的预期输出。Try
result.clear()
ing your stringstream before flushing it withresult.str("")
. This sets it to a state of accepting inputs again after outputting the buffer.Without clearing I get
test[0] == 0
andtest[1] == -832551553 /*some random number*/
. Withclear
I get the expected output oftest[0] == 0
andtest[1] == 1
.