天天看点

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();
           

输出结果是:

stingstream的数据格式转换

继续阅读