您的位置:首页 > 编程语言 > C语言/C++

在c++中如何将int转换成string,而不是char[]

2013-07-31 13:26 302 查看
在c++中如何将int转换成string,而不是char[]

第一种方法:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int n = 65535;
char t[256];
string s;

sprintf(t, "%d", n);
s = t;
cout << s << endl;

return 0;
}

第二种方法

//第二种方法
#include <iostream>
#include <string>
#include <strstream>
using namespace std;

int main()
{
int n = 65535;
strstream ss;
string s;
ss << n;
ss >> s;
cout << s << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: