在我的理解中,sstream是专门为字符串和其他数据类型转换用的。其实从sstream的用法中可以看出,说成流stream与其他数据类型转换更为恰当。最基本的用法是int和string的相互转换:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[])
{
stringstream stream;
// 将string转换为int
string text = "123456";
stream << text;
int n = 0;
stream >> n;
n++;
cout << n << endl;
text = "";
// 注意这句clear必不可少,注释掉之后stream的<<操作无效
stream.clear(); // 试试看注释掉?
// 将int转换为string
stream << n;
stream >> text;
// 加一句hello方便区分
text += "hello";
cout << text << endl;
system("pause");
return 0;
}
///////////////////////////End Sub//////////////////////////////////
码农场