C++ 如何清除字符串流中的内容。?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6939470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to clear the content in the String stream.?
提问by Balamurugan
can any one tell me how to clear the content of the stringstream..? i tried the following but it didnt work.
谁能告诉我如何清除字符串流的内容..?我尝试了以下但没有奏效。
stringstream ss;
ss<<"bala"<<"murugan";
cout<<ss.str(); //Output balamurugan
ss.str().clear();
ss<<" hi";
cout<<ss.str();
// output is balamurugan hi
my required output is " hi" alone.. Pls tell me
我需要的输出是“嗨”单独..请告诉我
回答by Seb Holzapfel
What you're doing is clearing the string that is returnedfrom the stringstream, not the internalstring. You'll have to actually do the ss.str("")
您正在做的是清除从 stringstream返回的字符串,而不是内部字符串。你必须真正做到ss.str("")
See here: How do you clear a stringstream variable?
请参阅此处: 如何清除 stringstream 变量?
回答by nirmus
The "clear()" member function is inherited from ios and is used to clear the error state of the stream.
“clear()”成员函数继承自ios,用于清除流的错误状态。
For clearing the contents of a stringstream, using:
要清除字符串流的内容,请使用:
stringstreamObject.str("");
回答by Nawaz
Do this:
做这个:
ss.str("");
This makes the stream empty!
这使得流为空!