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

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

 

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