Quick: Would you resort to stringstream? But that's so C++98... and so hard.
More people need to know about to_string
and its cousins. Marius Bancila takes us for a short tour:
The standard way of converting between numbers and strings in C++11
by Marius Bancila
From the article:
C++11 provides several standard functions for converting numbers to strings and strings to numbers, all of them available in the
<string>
header.For converting numbers to strings there are two new overloaded functions:
to_string()
andto_wstring()
. They take one argument of various numeric types (int
,long
,long long
,double
,long double
, etc.) and return either astd::string
or astd::wstring
with the number converted to text. ...For the other way around of converting strings to numbers there are several overloaded methods (taking either a
std::string
or astd::wstring
): ...
Add a Comment
Comments are closed.