Codejie's C++ Space

Using C++

StringOf()

    C++中,integer与string间的转换是躲不掉的,所以如何转换也就出来了各种的版本。像我是用std::ostringstream的。考虑到其性能,今天到网上找找,发现都差不多,只是有些写的好看的很。
    来源:http://forums.devx.com/archive/index.php/t-88692.html
Danny Kalev
06-16-200006:51 AM
Alex Oss wrote:
> or, more generally,
>
> template <class T>
> std::string StringOf(T object)
> {
> std::ostringstream os;
> os << object;
> return(os.str());
> }

I would change the above template 
as follows:

template 
<class T>
void StringOf(const T& object, std::string & s)
{
std::ostringstream os;
os 
<< object;
= os.str();
}

This version avoids copy by value of the argument and the 
string.

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http:
//www.amazon.com/exec/obidos/ASIN/0789720221

posted on 2009-03-31 13:41 codejie 阅读(269) 评论(0)  编辑 收藏 引用 所属分类: C++Resource


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

Using C++

导航

统计

留言簿(73)

随笔分类(513)

积分与排名

最新评论

阅读排行榜

评论排行榜