atoi and itoa conversions in C++11 -- FangLu
All your friends know about C++11's new stoi and to_string, right? If not, here's a quick refresher to share:
atoi and itoa conversions in C++11
by FangLu
The key reminder from the article:
... The
atoianditoaconversions 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::to_string,std::stoi/stol/stollare introduced to implementatoi/itoaconversions conveniently. For example:string s; s += to_string(12) + " is int, "; s += to_string(3.14f) + " is float."; cout << s << endl;where
to_stringcan do type conversion according to the parameter type.Here is another example:
string s("12"); int i = stoi(s); cout << i << endl;

CppCon minus one week:
In case you missed it:
Recently on the Spot blog: