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
atoi
anditoa
conversions 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/stoll
are introduced to implementatoi
/itoa
conversions conveniently. For example:string s; s += to_string(12) + " is int, "; s += to_string(3.14f) + " is float."; cout << s << endl;where
to_string
can do type conversion according to the parameter type.Here is another example:
string s("12"); int i = stoi(s); cout << i << endl;
Add a Comment
Comments are closed.