c++ string与int通过流相互转化

stringstream :字符串流  ,      需要包含头文件#include

stringstream使用操作符<< 、 >>,流在操作符的左边

>>:从流中输出东西

<<:输入东西到流中

#include 
#include 
#include 

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;
}

运行结果:

c++ string与int通过流相互转化_第1张图片

 

 

 

你可能感兴趣的:(c++)