[解决] terminate called after throwing an instance of 'std::out_of_range' wha...

编译运行程序遇到这样的问题:
terminate called after throwing an instance of ‘std:: out_of_range’ what(): basic_string::substr
内存读取越界。
解释1:
const std::string sTest( “test” );
sTest.substr( 0, 10 );
will raise the same exception, since you ask 10 characters, but only 5 ( sTest.length()) ) are available.
定义的sTest只有5个字符,要去sTest的字串(0,10),显然错误。

解释2:

std::string s(“foo”);

s.substr(5,1); //the length of the string is 3, 5 is out of bounds

同样会报错,out_of_range