C++11 added some new string conversion functions:
http://en.cppreference.com/w/cpp/string/basic_string/stoul
It includes stoi (string to int), stol (string t
I've no idea why stoi
exists but not stou
, but the only difference between stoul
and a hypothetical stou
would be a check that the result is in the range of unsigned
:
unsigned stou(std::string const & str, size_t * idx = 0, int base = 10) {
unsigned long result = std::stoul(str, idx, base);
if (result > std::numeric_limits::max()) {
throw std::out_of_range("stou");
}
return result;
}
(Likewise, stoi
is also similar to stol
, just with a different range check; but since it already exists, there's no need to worry about exactly how to implement it.)