连接String与Int(转)

来源:互联网 发布:淘宝单号购买平台 编辑:程序博客网 时间:2024/05/19 14:16

转自:http://blog.csdn.net/cywosp/article/details/8980633
方法一:

template<typename T>  static size_t Convert (char buf[], const T value)  {      static const char digits[] = "9876543210123456789";      static const char* zero = digits + 9;      T i = value;      char* p = buf;      do      {          int lsd = static_cast<int>(i % 10);          i /= 10;          *p++ = zero[lsd];      } while (i != 0);      if (value < 0)      {          *p++ = '-';      }      *p = '\0';      std::reverse (buf, p); // #include <algorithm>      return p - buf;  }  static inline void IntToString (std::string& out, const int value)  {      char buf[32];      Convert<int>  (buf, value);      out.append (buf);  }  

方法二:

static inline void IntToString (std::string& out, const int value)  {      char buf[32];      snprintf (buf, sizeof (buf), "%d", value);  // snprintf is thread safe #include <stdio.h>      out.append (buf);  }  

方法三:

static inline void IntToString (std::string& out, const int value)  {      std::strstream ss; // #include <strstream>      ss <<  value;      ss >> out;  }  

方法四:

static inline void IntToString (std::string& out, const int value)  {      char buf[32];      itoa (value, buf, 10); // #include <stdlib.h>      out.append (buf);  }  
0 0