标签:style blog os io ar 2014 sp 代码 log
stringstream利用流,可以对基本类型之间进行很方便的转化。
今天周赛标程里有,所以搜索了下,还觉得挺有用的,比sprintf好用,因为sprintf还要记住种种转换符号,烦都烦死了,这个利用流方便转化比较方便。
直接贴代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<sstream>
using namespace std;
int main()
{
int a=10,aa;
double b=5.15,bb;
string s="100",ss="100.5",str;
//char str[100];//也可以用char形字符串接收转换
char SS[100]="sdfk liwei yuting jdsfj";
stringstream s1,s2,s3,s4,s5;
s1<<a; //输入流中
s1>>str;//输出到字符串中,把整形转换为字符串
cout<<str<<endl;
s2<<b;
s2>>str;//把浮点形转换为字符串
cout<<str<<endl;
s3<<s;
s3>>aa;//把字符串转换为整形
cout<<aa<<endl;
s4<<ss;
s4>>bb;//把字符串转换为浮点形
cout<<bb<<endl;
//把单个字符串提取出来
s5<<SS;
string aim;
while(s5>>aim)
{
cout<<aim<<endl;
//依次输出"sdfk","liwei","yuting","jdsfj"字符串
}
return 0;
}
标签:style blog os io ar 2014 sp 代码 log
原文地址:http://blog.csdn.net/u011466175/article/details/39161667