清除流 [英] Clear a stream

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

问题描述

如何刷新strstream缓冲区,以便下次调用时.str()我

取回一个空字符串?


谢谢

How do I flush a strstream buffer so that the next time I call .str() I
get back an empty string?

Thanks

推荐答案

只需在其中加一个空字符串。


#include< cstdlib>

#include< sstream>

#include< iostream>


使用命名空间std;


int main(int argc,char * argv [])

{

stringstream s;


s << Blah;

cout<< 1: << s.str()<< "。" << endl;


s.str("");

cout<< 2: << s.str()<< "。" << endl;

system(PAUSE);

返回EXIT_SUCCESS;

}

Just put an empty string in it.

#include <cstdlib>
#include <sstream>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
stringstream s;

s << "Blah";
cout << "1: " << s.str() << "." << endl;

s.str("");
cout << "2: " << s.str() << "." << endl;
system("PAUSE");
return EXIT_SUCCESS;
}


Zachary Turner写道:
Zachary Turner wrote:
如何刷新strstream缓冲区,以便下次调用.str()时我会收回一个空字符串?
How do I flush a strstream buffer so that the next time I call .str() I
get back an empty string?




你真的需要使用旧的基于char *的流吗?在字符串流中,

(std :: string-based stream)你只需要做什么


ss.str(""); ss.clear();


,没关系。旧的(strstream)很可能不会那样工作。


V



Do you really need to use the old char* based streams? In a stringstream,
(std::string-based stream) you simply do

ss.str(""); ss.clear();

and it''s fine. The old (strstream) most likely won''t work like that.

V


是的,我实际上是指stringstream。我不会使用strstream。无论如何

很有趣,解决方案是如此微不足道。我在MSDN帮助中查看了很长时间

(使用Visual C ++),对上帝说实话,并没有提到

str()函数的重载。我输入的速度非常快我从来没有注意到当我输入时会发出intellisense(看到它有一个

重载版本。我已经一直想知道这个问题的时间比我要承认的还要多,但它从来没有影响到我在一个论坛上不得不问我的价值。现在我感到愚蠢,呵呵。

Yeah I actually meant stringstream. I don''t use strstream. Anyway
funny that the solution is so trivial. I looked in MSDN help for ages
(using Visual C++) and it honest to god just doesn''t mention that
overload of the str() function. And I type so fast I''ve just never
noticed the intellisense pop up when i type the ( to see that it has an
overloaded version. I''ve been wondering about that question for longer
than I care to admit but it never affected me to the point I had to ask
on a forum. Now I feel dumb, hehe.


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

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