stringstream 用法
作者:
假如有点困
,
2022-08-24 13:31:11
,
所有人可见
,
阅读 293
字符串转数字:
stringstream stream;
string result="10000";
int n=0;
stream<<result;
stream>>n;
数字转字符串:
stringstream stream;
string result;
int i = 1000;
stream << i;
stream >> result;
stringstream构造函数用法:
string result="10000";
stringstream stream(result);(可以是字符串也可以是数字,总之后面直接输入到目标变量里面)
int n=0;
stream>>n;
去空格和换行(因为stringstream是通过空格判断一个字符串的结束):
string str="i am a boy";
stringstream stream(str);
string s;
while(stream>>s)
{
cout<<s<<endl;
}
输出结果:
i
am
a
boy
注意事项:
一个程序最好只使用一个字符串流,因为构造它很慢,所以在每次使用字符串流前,先clear清下缓存。