Chinaunix

标题: 怎么将int转化成string呢?in c++ [打印本页]

作者: ZDer    时间: 2004-10-24 08:25
标题: 怎么将int转化成string呢?in c++
请问有什么简单方法把int转换成string呢
作者: yych    时间: 2004-10-24 09:23
标题: 怎么将int转化成string呢?in c++
CString s;
int i=10;
s.format("%d",i);
作者: THEBEST    时间: 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. }
复制代码

作者: npn9014    时间: 2004-10-26 21:23
标题: 怎么将int转化成string呢?in c++
sprint()不可以吗?
作者: Yerk    时间: 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. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2