Post

C++ Style Int or Float to String conversion

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:

1
2
3
#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)

This post is licensed under CC BY-ND 4.0 by the author.