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;
s = 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