stingstream的数据格式转换

如果想从字符串中提取 整形 浮点型 等数据可以通过stringstream来转换。#include<sstream>

在使用stringsteam时注意对内存的处理。

例如:

         int circle=3;
	stringstream test;
	string str;
	int num;
	float f; 
	while(circle){
		circle--;
		test.clear();//不清除任何内容,仅仅重置了流的状态标志
		test.str("");//清除流的缓存,每次循环内存大小将不再增加,假如没有这句,内存大小将增加,可以通过看test的大小得出结论
		test<<"123sss";
		test>>str;

		cout<<"size of the stream is "<<test.str().size()<<endl;

	}
	test.clear();
	test.str("");
	test<<"45 sss 12.4";
	test>>num>>str>>f;
	cout<<"number is "<<num<<" and "<<f<<endl;
	cout<<"string is "<<str<<endl;
	getchar();


输出结果是

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值