stringstream :字符串流 , 需要包含头文件#include<sstream>
stringstream使用操作符<< 、 >>,流在操作符的左边
>>:从流中输出东西
<<:输入东西到流中
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
/*字符串转化为数字*/
string s;
cin >> s; //从流(cin)中输出东西到字符串s中
stringstream st; //字符串流
st << s; //输入s字符串到流(st)中
int n;
st >> n; //从流(st)中输出东西到n;
cout << n << endl; //输入n到流(cout)中
/*数字转化为字符串*/
int m = 12345;
stringstream st2;
st2 << m;
string s2;
st2 >> s2;
cout << s2 << endl;
system("pause");
return 0;
}
运行结果: