参考链接:http://www.cppblog.com/forLinda/archive/2006/03/17/4298.html
法一
/**
* <stdio.h>
* int sprintf( char *buffer, const char *format [, argument] ... );
*/
void int2str0() {
int ss;
char temp[64];
string str;
ss = 87676990;
sprintf(temp, "%d", ss);
string s(temp);
cout << s.c_str() << endl; // 87676990
cout << s.size() << endl; // 8
}
法二
/**
* <stdlib.h>
* char *_itoa( int value, char *string, int radix );
*/
void int2str1() {
char buf[20];
int i = 3445;
_itoa(i, buf, 10);
string s(buf);
}
法三
/**
* <sstream.h> or <sstream>
*/
void int2str2() {
int hello = 4;
stringstream ss;
ss << hello;
string s = ss.str();
cout << s.c_str() << endl;
}
法四
string int2str(int num) {
string str = "";
if (num == 0) {
return "0";
}
bool bNegative = (num < 0);
num = abs(num);
while (num) {
str = (char)(num % 10 + 48) + str ;
num /= 10;
}
return bNegative ? "-" + str : str;
}