Std::out_of_range for stoi()

The overflow exception while using stoi() function in cpp. Please explain the case for the example I am adding:

string s = “112233445566”;
int i = stoi(s,0,10);

The exception occurs for the integer ‘i’ upon conversion using stoi() however, not for the string ‘s’. The out_of_range error is being issued for only ‘i’ and not for ‘s’. Why? Please elaborate upon this.

First look here.
Then RTFM.
And this as a bonus.

2 Likes

I’m sorry but I can’t find the answer on the site you have mentioned. Could you please share the your answer? Meaning no references. Pure explanation.

int maximum value is 2147483647 (most of the time, it can depends of the compiler/system).
You are trying to put 112233445566 into an int. It is not possible, so stoi throws a out_of_range exception.

1 Like