stringstream中的.clear()和.str()

在C++中可以使用stringstream来很方便的进行类型转换,字符串串接,不过注意重复使用同一个stringstream对象时要先继续清空,而清空很容易想到是clear方法,而在stringstream中这个方法实际上是清空stringstream的状态(比如出错等),真正清空内容需要使用.str(“”)方法。

用.str(“”)方法可以清楚缓存,但是,需要重复使用同一个stringstream对象时,得先用.str(“”)方法清楚缓存,再用.clear()方法重设状态,否则对stringstream对象的操作是无效的。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	stringstream stream;
	stream<<"123";
	int n;
	stream>>n;
	cout<<n<<endl;
	//stream.str("");
	stream.clear();
	stream<<"de";
	string s;
	stream>>s;
	cout<<s;
	return 0;
}

输出结果:

而不用.str(“”)方法,只用.clear()方法,可以得到同上的结果,但是这是很危险的,极有可能耗尽全部内存。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	stringstream stream;
	stream<<"123";
	int n;
	stream>>n;
	cout<<n<<endl;
	stream.str("");
	//stream.clear();
	stream<<"de";
	string s;
	stream>>s;
	cout<<s;
	return 0;
}

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	stringstream stream;
	stream<<"123";
	int n;
	stream>>n;
	cout<<n<<endl;
	//stream.str("");
	//stream.clear();
	stream<<"de";
	string s;
	stream>>s;
	cout<<s;
	return 0;
}

时间: 2024-08-08 13:58:16

stringstream中的.clear()和.str()的相关文章

stringstream中的clear()与str()

今天在用stringstream做数据转换的时候,遇到了问题,发现得到的不是预期的结果. 简化的代码如下: #include <cstdlib> #include <iostream> #include <sstream> using namespace std; int main(int argc, char * argv[]) { stringstream stream; int a,b; stream<<"80"; stream&g

C++中cin.clear()的用法

我们谈谈cin.clear的作用,第一次看到这东西,很多人以为就是清空cin里面的数据流,而实际上却与此相差很远,首先我们看看以下代码: #include <iostream>  using namespace std;  int main()   {              int a;              cin>>a;              cout<<cin.rdstate()<<endl;              if(cin.rds

istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string

 1. istringstream字符串流 #include <iostream> #include <sstream> #include <string> using namespace std; struct MyStruct { string str1, str2, str3; double db; int num; char ch; }; void main() { string  mystring("china  google microsoft

vector 中的clear()

为什么clear之后,还是输出fdsafdsa.有什么办法可以真正清空之? 因为对于vector,clear并不真正释放内存(这是为优化效率所做的事),clear实际所做的是为vector中所保存的所有对象调用析构函数(如果有的话),然后初始化size这些东西,让你觉得把所有的对象清除了... 真正释放内存是在vector的析构函数里进行的,所以一旦超出vector的作用域(如函数返回),首先它所保存的所有对象会被析构,然后会调用allocator中的deallocate函数回收对象本身的内存.

css中的clear:both,display:flex;

介绍两者一起讨论的原因: 在明天就国庆的日子里陪着程序员的只有代码,啤酒,还有音乐,然后就是灯光下默默陪伴自己的影子.好了,不矫情了. ----------------------------------------------------------- 先上两张图,然后慢慢道来...... 第一张是实现的布局,第二张是布局的代码.简单的说明一下:图一中有4块为classname为newsItem的div容器(代码有点乱看起来有点吃力),这4个容器包含在classname为newsRow的sec

c++中while(cin&gt;&gt;str)和ctrl z的相关问题探讨

对于while (cin>>str)和ctrl z的问题,网上有以下解释: ------------------------------------------------------------------------------------------------------------------------------ 输入(cin)缓冲是行缓冲.当从键盘上输入一串字符并按回车后,这些字符会首先被送到输入缓冲区中存储.每当按下回车键后,cin就会检测输入缓冲区中是否有了可读的数据. c

delphi 中TStringList Clear 方法的时候该对象有没有被释放

delphi 中TStringList 通过function AddObject(const S: string; AObject: TObject): Integer; 方法添加了一个对象,请问我在调用Clear 方法的时候该对象有没有被释放 object里存的只是指向对象的指针,clear只是把指针清除了,对象并没有被释放.TObjectList可以自动释放对象,剩下的TList,StringList等List类型的都需要手动释放.

jsp中利用response.senddirect(str)重定向,传递参数新思路

用Servlet进行请求重定向,参数传递好办,直接用request.setAttribute(str1,str2); 但是如果不用Servlet 而是直接用jsp进行转发呢? 我们首先要知道   请求的重定向:在最终的Servlet中,request对象和中转的那个request不是同一个对象 所以传递参数,自然就获取不到了 下面我们换思路,另辟蹊径,我们用session  session的生命周期长啊 所以完全可以获取 至于session与request的知识,请查阅相关资料 下面贴出登录出现

C#的cs文件中Response.Clear();Response.ClearHeaders()的作用

在学习一个CS文件,如下:public partial class GetPic : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        string picid = Request.QueryString["id"];        string pictureFilename = Path.Combine(Server.MapPath("/produ