The stoi
function of c++ is defined as:
int stoi(const std::string& str, std::size_t* pos = 0, int base = 10);
as you can see, the base
argument is defaulted as 10
, so by default it could only handle decimal numbers. By setting base
to 0
, it could handle numbers by their prefixes. This is same behavior as strtol
, so why is the default value being set to 10
, rather than 0
?
stoi("000123456")
to yield a decimal number (just trim the leading zeros) instead of making it an octal value with the leading zeros trimmed off. – Makebelieve