equivalent of atoi

Mansuro picture Mansuro · Aug 13, 2011 · Viewed 26.9k times · Source

Is there a function that could replace atoi in c++. I made some research and didn't find anything to replace it, the only solutions would be using cstdlib or implementing it myself

Answer

David Rinck picture David Rinck · May 27, 2014

If you don't want to use Boost, C++11 added std::stoi for strings. Similar methods exist for all types.

std::string s = "123"
int num = std::stoi(s);

Unlike atoi, if no conversion can be made, an invalid_argument exception is thrown. Also, if the value is out of range for an int, an out_of_range exception is thrown.