Why is the return value of string. find ()-1?
It seems that I have seen this problem for a long time before. I cannot remember it. I encountered this problem again when I wrote a string today. The definition in the book is "string. when find () is not found, a large value is returned. Some people on the Internet say it is-1, both of which are true, because integers are stored in the computer in the form of supplementary codes, if the second digit of a high value is 1 when one integer is less than one, then the second digit is negative, if the second digit of A High integer includes all 1 after the second digit, then-1 is used in the case of one less digit. String. the large number returned by find () is the power 32 of 2, while the int and long integer expressed in 4 bytes can only represent the power 31 of 2 at most, the value of the last 32 bits stored by the power of 2 is exactly the same as the complement code of-1: 11111111 11111111 11111111 11111111; if the value is assigned to an int or long type, it is naturally-1.
The following is the C ++ verification code.
# Include
# Include
Using namespace std; int main () {string str = "helloworld! "; Int I; long l; I = str. find (" y "); l = str. find (" y "); cout <
The result is-1-1 4294967295
4294967295 minus one for 32 places equal to 2