Archive for December, 2010

C++ Style Int or Float to String conversion

December 20, 2010

If you want to convert an int or float/double value into a string representation without using snprintf or itoa, here’s the C++ way:

#include <sstream>

template <typename T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}

Of course you can also use the stringstream variable the same way as you would cout.
(found on the net)

Advertisements