用stringstream轉換,代碼如下:
#include<iostream>
#include<string>
#include <sstream>
using namespace std;
int main()
{
int i = 32425;
string str;
stringstream stream2;//注意一次轉換用一個不同的流
stream2 << i; //向stream中插入整型數1234
stream2 >> str; //從steam中提取剛插入的整型數 并将其賦予變量str完成整型數到string的轉換
cout << str << endl; //輸出str
return 0;
}
我又把它打包成了itos函數,與stoi函數相對應,代碼如下:
string itos(int i){
string str;
stringstream stream;
stream << i;
stream >> str;
return str;
}
參考:
https://blog.csdn.net/ly52352148/article/details/52016933