The prototype of this function is substr (size_type off, size_type count)
The first parameter indicates the offset in the string char array. Count is the length of the string starting from the offset.
The incorrect use of these two parameters will cause crash or incorrect results.
There are two major errors: off is greater than the array length or less than 0, and count is greater than the string length or count is less than 0
For the off error, when it is greater than the length of the array, that is, the array out-of-bounds error, the program crash.
When the provided off is less than 0, the parameter type is unsigned int, so it will be forced to convert off into a large integer, which leads to an out-of-bounds error and program crash.
For the Count error, when count + off is greater than the length of the array, substr first checks whether it exceeds the length of the array. If it exceeds the length, it will be processed according to the maximum subscript of the array. Therefore, it is equivalent to substr (Off, String. Size ()-off );
Similarly, when count is less than 0, it is converted to a large integer, and the result is equivalent to substr (Off, String. Size ()-off );
In summary, when using this function, you must pay attention to the input parameters to prevent program crash or get incorrect results.