Problem: I have an integer; this integer needs to be converted to type stl :: string.
In the past, I used stringstream to convert, and it's just cumbersome. I know that the C-way is to do sprintf , but I'd rather do C ++ - a method that is typafe (er).
Is there a better way to do this?
Below is the stringstream method I used in the past:
std::string intToString(int i) { std::stringstream ss; std::string s; ss << i; s = ss.str(); return s; }
Of course, this could be rewritten as follows:
template<class T> std::string t_to_string(T i) { std::stringstream ss; std::string s; ss << i; s = ss.str(); return s; }
However, I have an opinion that this is a rather "hard" implementation.
Zan noticed that the call is pretty nice:
std::string s = t_to_string(my_integer);
Anyway, the best way would be ... nice.
Connected:
Alternative to itoa () to convert integer to C ++ string?
c ++ stdstring integer
Paul Nathan Nov 07 '08 at 22:52 2008-11-07 22:52
source share