C++ int转string

刺骨的言语ヽ痛彻心扉 2021-12-16 10:37 376阅读 0赞

不论是过去写的这个转换方法,还是今天看到的这个:

  1. string cvt2str( int x )
  2. {
  3. int d = x;
  4. string ans = "";
  5. while( x > 0 )
  6. {
  7. d = x%10;
  8. ans = char(d+'0')+ans;
  9. x /= 10;
  10. }
  11. return ans;
  12. }

转换方法,都不是很好。

我最常用的方法是这样:

  1. #include <sstream>
  2. std::string int2s(int num)
  3. {
  4. std::stringstream ss;
  5. ss<<num;
  6. std::string re;
  7. ss>>re;
  8. return re;
  9. }

转载于:https://www.cnblogs.com/tiandsp/p/3867937.html

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JS中Stringint

    方案一代码:   Number(str) 方案二代码:  // parseInt 方法都有两个参数,第一个参数就是要转换的对象,第二个参数是进制基数,可以是2,8