StringOf()

StringOf()

    C++中,integer与string间的转换是躲不掉的,所以如何转换也就出来了各种的版本。像我是用std::ostringstream的。考虑到其性能,今天到网上找找,发现都差不多,只是有些写的好看的很。
    来源: http://forums.devx.com/archive/index.php/t-88692.html
Danny Kalev
06 - 16 - 2000 06 : 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

你可能感兴趣的:(StringOf())