1、使用stringstream
#include <bits/stdc++.h>
using namespace std;
int main()
{
int age=20;
stringstream s;
s<<age;
string s2;
s>>s2;
cout<<s2;
return 0;
}
2、使用to_string
#include <bits/stdc++.h>
using namespace std;
int main()
{
int age;
string s;
s=to_string(age);
cout<<s;
return 0;
}
注意事项:如果你使用的是devc++,运行时to_string可能会报错,这是你需要进行编译器配置,
过程如下:工具-->编译器选项-->代码生成/优化-->代码生成-->语言标准-->选择C++11
如图所示: