一、int转string
c++11标准增加的全局函数std::to_string(参数)。
参数类型可以是:int、long、long long、unsigned、unsigned long、unsigned long long、float、double、long double。
to_string函数的实现:
//实现to_string函数 #include<iostream> #include<string> using namespace std; #define max 100 string to_String(int n) { int m = n; char s[max]; char ss[max]; int i=0,j=0; if (n < 0)// 处理负数 { m = 0 - m; j = 1; ss[0] = '-'; } while (m>0) { s[i++] = m % 10 + '0'; m /= 10; } s[i] = '\0'; i = i - 1; while (i >= 0) { ss[j++] = s[i--]; } ss[j] = '\0'; return ss; } int main() { cout << "请输入整数:"; int m; cin >> m; string s = to_String(m) + "abc"; cout << s << endl; system("pause"); return 0; }
标签:类型转换,string,int,max,unsigned,long,ss From: https://www.cnblogs.com/ekux127/p/16997699.html