Let's take a look at the code that implements KMP:
1 voidGetNext (int* Next,stringstr) {2 intI=0, j=-1;3next[0]=-1;4 while(I < str.length ()-1){5 if(j==-1|| str[i]==Str[j]) {6i++;7J + +;8next[i]=J;9 }Ten Else{ Onej=Next[j]; A } - } - } the intKmpmatch (stringBufferstringp) { - intnext[ -]; - GetNext (next,p); - intI=0, j=0; + while(I < Buffer.length () && J <p.length ()) { - if(j = =-1|| Buffer[i] = =P[j]) { +i++; AJ + +; at } - Else -j=Next[j]; - } - if(J = =p.length ()) - returnI-J; in Else - return-1; to}
It seems that everything is fine, but the actual operation is return-1!
Debug looked under, each time when in I=0,j=-1, for
while (I < Buffer.length () && J < P.length ())
The decision condition is not established, jump out of the while loop, directly execute 27 rows of if (j = = P.length ()) ?
Then look at the declaration of the length function, the return type is size_t!! is an unsigned number!!
Const;
"C + + Primer" inside the hint: do not mix with signed and unsigned types
If the expression has both a signed type and an unsigned type, the exception result occurs when the signed type has a negative value, because the signed number is automatically converted to the unsigned number.
When the symbol number J and the unsigned number string::length () are compared, j is automatically converted to an unsigned integer, and J is converted directly to 4294967295 (on my platform, the result depends on the number of digits of your machine's int)!! Causes the while loop to jump out directly, returning the wrong result.
Although some of the programming iron you have seen, but soon may forget or unconsciously violate these principles, or only step on to remember these pits. So still taking advantage of the work before more code, not afraid to make mistakes, it is best to be wrong unforgettable, more summary.
Enclose the C + + type auto-conversion rule:
1. In an expression, the values of char and short type, whether signed or unsigned, are automatically converted to int or unsigned int (if the size of short is the same as int, the range of unsigned short is greater than int, in which case Unsigned short is converted to unsigned int). Because they are converted to a type that represents a larger range, the conversion is referred to as an "upgrade (promotion)".
2. Rank the various data types in order from highest to lowest: long double, double, float, unsigned long long, long long, unsigned long, long, unsigned int, and Int. There is a small exception, if long and int are the same size, then the unsigned int should be above the long. Char and short do not appear in this rank list because they should have been upgraded to int or unsigned int.
3. In any operation involving two data types, the lower rank types are converted to higher-level types.
4. In the assignment statement, = the value on the right is the first to convert the data type of the right value to the type of the left variable before giving = to the left variable. That is, the left-hand variable is the data type, and the value on the right is converted to the value of what data type. This process may cause the type of value on the right to be upgraded, or it may cause its type demotion (demotion). The so-called "demotion" refers to a higher-level type being converted to a lower-level type.
5. When passed as a parameter to a function, char and short are converted to int,float and converted to double. This automatic upgrade can be avoided by using function prototypes.
A trap for automatic type conversion in C + +