The first parameter of C++ STL function substr(pos,n), pos, is said to have this behaviour.
“If the position passed is past the end of the string, an out_of_range exception is thrown.”
However, if I do something like
string s("ab");
cout<<s.substr(2,666)<<endl;
then no exception is thrown even though the pos=2 is by definition past the end of the string.
The string::end defines the position “after the last character in the string” as “past the end of the string.”
I noticed the returned character is always the ‘\0’. My question is if this is standard behaviour and if I can count on the fact that an empty string is returned in this case. Thank you.
The actual requirement is (§21.4.7.8):
In your case, pos == size(), so you should never see an exception, and should always get an empty string.