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实现.
namespace boost
{
// exception used to indicate runtime lexical_cast failure
class bad_lexical_cast : public std::bad_cast
{
public:
bad_lexical_cast() :
source(&typeid(void)), target(&typeid(void))
{
}
bad_lexical_cast(
const std::type_info &s,
const std::type_info &t) :
source(&s), target(&t)
{
}
const std::type_info &source_type() const
{
return *source;
}
const std::type_info &target_type() const
{
return *target;
}
virtual const char *what() const throw()
{
return "bad lexical cast: "
"source type value could not be interpreted as target";
}
virtual ~bad_lexical_cast() throw()
{
}
private:
const std::type_info *source;
const std::type_info *target;
};
namespace detail // selectors for choosing stream character type
{
template<typename Type>;
struct stream_char
{
typedef char type;
};
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<>;
struct stream_char<wchar_t>;
{
typedef wchar_t type;
};
template<>;
struct stream_char<wchar_t *>;
{
typedef wchar_t type;
};
template<>;
struct stream_char<const wchar_t *>;
{
typedef wchar_t type;
};
template<>;
struct stream_char<std::wstring>;
{
typedef wchar_t type;
};
#endif
template<typename TargetChar, typename SourceChar>;
struct widest_char
{
typedef TargetChar type;
};
template<>;
struct widest_char<char, wchar_t>;
{
typedef wchar_t type;
};
}
namespace detail // stream wrapper for handling lexical conversions
{
template<typename Target, typename Source>;
class lexical_stream
{
public:
lexical_stream()
{
stream.unsetf(std::ios::skipws);
if(std::numeric_limits<Target>;::is_specialized)
stream.precision(std::numeric_limits<Target>;::digits10 + 1);
else if(std::numeric_limits<Source>;::is_specialized)
stream.precision(std::numeric_limits<Source>;::digits10 + 1);
}
~lexical_stream()
{
#if defined(BOOST_NO_STRINGSTREAM)
stream.freeze(false);
#endif
}
bool operator<<(const Source &input)
{
return !(stream << input).fail();
}
template<typename InputStreamable>;
bool operator>;>;(InputStreamable &output)
{
return !is_pointer<InputStreamable>;::value &&
stream >;>; output &&
(stream >;>; std::ws).eof();
}
bool operator>;>;(std::string &output)
{
#if defined(BOOST_NO_STRINGSTREAM)
stream << '\0';
#endif
output = stream.str();
return true;
}
#ifndef DISABLE_WIDE_CHAR_SUPPORT
bool operator>;>;(std::wstring &output)
{
output = stream.str();
return true;
}
#endif
private:
typedef typename widest_char<
typename stream_char<Target>;::type,
typename stream_char<Source>;::type>;::type char_type;
#if defined(BOOST_NO_STRINGSTREAM)
std::strstream stream;
#elif defined(BOOST_NO_STD_LOCALE)
std::stringstream stream;
#else
std::basic_stringstream<char_type>; stream;
#endif
};
}
template<typename Target, typename Source>;
Target lexical_cast(Source arg)
{
detail::lexical_stream<Target, Source>; interpreter;
Target result;
if(!(interpreter << arg && interpreter >;>; result))
throw_exception(bad_lexical_cast(typeid(Target), typeid(Source)));
return result;
}
}
复制代码
作者:
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
下编译通过:
#include <string>;
#include <sstream>;
using namespace std;
int main(int argc, char *argv[]) {
int test = 10;
string line = "";
ostringstream ost(line);
ost << test;
cout << ost.str() << endl;
return 0;
}
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2