c++中的string转换

如何转换string到其他类型?如何从其他类型转换到string?
代码1:toString
template <class T> std::string toString(const T& a)
{
std::ostringstream ost;
ost << a;
return ost.str();
}

代码2:fromString
template<typename T>T fromString( const std::string& s)
{
std::istringstream is(s);
T t;
is >> t;
return t;
}

使用方法:
int d = fromString<int>( s );
string str=toString(d);

参考:

http://www.thescripts.com/forum/thread62219.html

This entry was posted in Program and tagged , , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please copy the string KvGJDb to the field below:

以新浪微博帐号登录