int转为string
- 利用sstream类
cpp
复制代码
#include <iostream>
#include <sstream>
using namespace std;
int main(){
int n = 10;
stringstream ss;
string str;
ss << n;
ss >> str;
cout << str << endl;
}
- sprintf
cpp
复制代码
#include <iostream>
#include <string>
using namespace std;
int main(){
int n = 10;
char t[15];
sprintf(t, "%d", n); // 转成char类型
cout << t << endl;
string str(t); //转成string类型
cout << str << endl;
}
- to_string
c++的版本的是C++11
cpp
复制代码
#include <iostream>
#include <string>
using namespace std;
int main(){
int m = 10;
string str;
str = to_string(m);
cout << str <<endl;
}