免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 20493 | 回复: 4
打印 上一主题 下一主题

[C++] 怎么将int转化成string呢?in c++ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-10-24 08:25 |只看该作者 |倒序浏览
请问有什么简单方法把int转换成string呢

论坛徽章:
0
2 [报告]
发表于 2004-10-24 09:23 |只看该作者

怎么将int转化成string呢?in c++

CString s;
int i=10;
s.format("%d",i);

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
3 [报告]
发表于 2004-10-24 13:10 |只看该作者

怎么将int转化成string呢?in c++

可以去看看boost中的lexical_cast实现.
  1. namespace boost
  2. {
  3.     // exception used to indicate runtime lexical_cast failure
  4.     class bad_lexical_cast : public std::bad_cast
  5.     {
  6.     public:
  7.         bad_lexical_cast() :
  8.         source(&typeid(void)), target(&typeid(void))
  9.         {
  10.         }
  11.         bad_lexical_cast(
  12.             const std::type_info &s,
  13.             const std::type_info &t) :
  14.             source(&s), target(&t)
  15.         {
  16.         }
  17.         const std::type_info &source_type() const
  18.         {
  19.             return *source;
  20.         }
  21.         const std::type_info &target_type() const
  22.         {
  23.             return *target;
  24.         }
  25.         virtual const char *what() const throw()
  26.         {
  27.             return "bad lexical cast: "
  28.                    "source type value could not be interpreted as target";
  29.         }
  30.         virtual ~bad_lexical_cast() throw()
  31.         {
  32.         }
  33.     private:
  34.         const std::type_info *source;
  35.         const std::type_info *target;
  36.     };

  37.     namespace detail // selectors for choosing stream character type
  38.     {
  39.         template<typename Type>;
  40.         struct stream_char
  41.         {
  42.             typedef char type;
  43.         };

  44.         #ifndef DISABLE_WIDE_CHAR_SUPPORT
  45.         template<>;
  46.         struct stream_char<wchar_t>;
  47.         {
  48.             typedef wchar_t type;
  49.         };

  50.         template<>;
  51.         struct stream_char<wchar_t *>;
  52.         {
  53.             typedef wchar_t type;
  54.         };

  55.         template<>;
  56.         struct stream_char<const wchar_t *>;
  57.         {
  58.             typedef wchar_t type;
  59.         };

  60.         template<>;
  61.         struct stream_char<std::wstring>;
  62.         {
  63.             typedef wchar_t type;
  64.         };
  65.         #endif

  66.         template<typename TargetChar, typename SourceChar>;
  67.         struct widest_char
  68.         {
  69.             typedef TargetChar type;
  70.         };

  71.         template<>;
  72.         struct widest_char<char, wchar_t>;
  73.         {
  74.             typedef wchar_t type;
  75.         };
  76.     }
  77.    
  78.     namespace detail // stream wrapper for handling lexical conversions
  79.     {
  80.         template<typename Target, typename Source>;
  81.         class lexical_stream
  82.         {
  83.         public:
  84.             lexical_stream()
  85.             {
  86.                 stream.unsetf(std::ios::skipws);

  87.                 if(std::numeric_limits<Target>;::is_specialized)
  88.                     stream.precision(std::numeric_limits<Target>;::digits10 + 1);
  89.                 else if(std::numeric_limits<Source>;::is_specialized)
  90.                     stream.precision(std::numeric_limits<Source>;::digits10 + 1);
  91.             }
  92.             ~lexical_stream()
  93.             {
  94.                 #if defined(BOOST_NO_STRINGSTREAM)
  95.                 stream.freeze(false);
  96.                 #endif
  97.             }
  98.             bool operator<<(const Source &input)
  99.             {
  100.                 return !(stream << input).fail();
  101.             }
  102.             template<typename InputStreamable>;
  103.             bool operator>;>;(InputStreamable &output)
  104.             {
  105.                 return !is_pointer<InputStreamable>;::value &&
  106.                        stream >;>; output &&
  107.                        (stream >;>; std::ws).eof();
  108.             }
  109.             bool operator>;>;(std::string &output)
  110.             {
  111.                 #if defined(BOOST_NO_STRINGSTREAM)
  112.                 stream << '\0';
  113.                 #endif
  114.                 output = stream.str();
  115.                 return true;
  116.             }
  117.             #ifndef DISABLE_WIDE_CHAR_SUPPORT
  118.             bool operator>;>;(std::wstring &output)
  119.             {
  120.                 output = stream.str();
  121.                 return true;
  122.             }
  123.             #endif
  124.         private:
  125.             typedef typename widest_char<
  126.                 typename stream_char<Target>;::type,
  127.                 typename stream_char<Source>;::type>;::type char_type;

  128.             #if defined(BOOST_NO_STRINGSTREAM)
  129.             std::strstream stream;
  130.             #elif defined(BOOST_NO_STD_LOCALE)
  131.             std::stringstream stream;
  132.             #else
  133.             std::basic_stringstream<char_type>; stream;
  134.             #endif
  135.         };
  136.     }

  137.     template<typename Target, typename Source>;
  138.     Target lexical_cast(Source arg)
  139.     {
  140.         detail::lexical_stream<Target, Source>; interpreter;
  141.         Target result;

  142.         if(!(interpreter << arg && interpreter >;>; result))
  143.             throw_exception(bad_lexical_cast(typeid(Target), typeid(Source)));
  144.         return result;
  145.     }
  146. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2004-10-26 21:23 |只看该作者

怎么将int转化成string呢?in c++

sprint()不可以吗?

论坛徽章:
0
5 [报告]
发表于 2004-10-26 22:19 |只看该作者

怎么将int转化成string呢?in c++

以下代码在
gcc version 2.95.4 20020320 [FreeBSD]

Visual studio .net 2004
下编译通过:

  1. #include <string>;
  2. #include <sstream>;
  3. using namespace std;

  4. int main(int argc, char *argv[]) {
  5.     int test = 10;
  6.     string line = "";
  7.     ostringstream ost(line);
  8.     ost << test;
  9.     cout << ost.str() << endl;
  10.     return 0;
  11. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP