如何清除stringstream? [英] How to clear stringstream?

查看:144
本文介绍了如何清除stringstream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stringstream parser;

parser << 5;
short top = 0;
parser >> top;
parser.str(""); //HERE I'M RESETTING parser

parser << 6; //DOESN'T PUT 6 INTO parser
short bottom = 0;
parser >> bottom;

为什么不起作用?

推荐答案

通常情况下,'reset'一个字符串流你需要将基础序列重置为一个空字符串 str 清除。

Typically to 'reset' a stringstream you need to both reset the underlying sequence to an empty string with str and to clear any fail and eof flags with clear.

parser.str( std::string() );
parser.clear();

通常会发生的是第一个>> 到达字符串的末尾并设置eof位,虽然它成功解析了第一个短。在此之后的流上的操作立即失败,因为流的eof位仍然设置。

Typically what happens is that the first >> reaches the end of the string and sets the eof bit, although it successfully parses the first short. Operations on the stream after this immediately fail because the stream's eof bit is still set.

这篇关于如何清除stringstream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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