C++ int转string
第一种方式
#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++中将整型变量转换为字符串的方法:一是使用sprintf结合字符串复制;二是利用strstream进行流操作实现类型转换。两种方法各有优劣,第一种更为常见。
3612

被折叠的 条评论
为什么被折叠?



