All your friends know about C++11's new
to_string, right? If not, here's a quick refresher to share:
The key reminder from the article:
itoaconversions in C are not very satisfying to programmers, because programmers need to deal with invalid input and exceptions to avoid worst case. On the other hand, these functions are straightforward and easy to use. So they are not rare in C++ code...
In C++11, global functions, such as
std::stoi/stol/stollare introduced to implement
itoaconversions conveniently. For example:string s; s += to_string(12) + " is int, "; s += to_string(3.14f) + " is float."; cout << s << endl;
to_stringcan do type conversion according to the parameter type.
Here is another example:string s("12"); int i = stoi(s); cout << i << endl;