- stringstream重复使用时,需要清空,否则会发生错误.
例:
int a, b;
stringstream now;
now << "100";
now >> a;
cout << a << endl;
//重复使用需清空stringstream
now.clear();
now << "200";
now >> b;
cout << b << endl;
- 将 int 类型转换为 string 类型 :
int value = 123456;
string s;
stringstream now;
now << value;
now >> s;
printf("%s", s);
- 提取string类型中的所有以空格(空格可以是若干个)分隔的整数:
string s = "15 201 123 4567 10000";
stringstream now;
now << s;
int x;
while(now >> x) {
cout << x << endl;
}
运行结果:
- 任意类型之间的转换:
{
stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
return result;
}