请不要调用现有的函数,怎么把int-->char*?

面试题 给你一个int型数据,如何将其转化成string类型,请不要调用 现有的函数,自己写,不会写就不用进来了

跃然一笑
浏览 122回答 2
2回答

泛舟湖上清波郎朗

int int_a;string string_a;char temp[128];int_a=10;sprintf(temp,"%d",int_a);string_a=(string)(temp);先将整型转换成char[]字符串,然后再转换成string咯

www说

#define MAX_DIGITS_INT 10void IntToStr( int num ,char str[] ){int i = 0, j = 0;int isNeg = 0;char temp[MAX_DIGITS_INT + 2];if( num<0 ){num* = -1;isNeg = 1;}do{temp[i++] = (num%10) + '0';num/ = 10;}while(num);if( isNeg )temp[i++] = '-1';while(i>0)str[j++] = temp[--i];str[j] = '\0';}
打开App,查看更多内容
随时随地看视频慕课网APP