The return values of the length and size functions in string (also the strlen function that measures the string in char)
It should be of the unsigned int type.
It cannot be compared with-1.
Avoid comparing unsigned int and INT data whenever possible.
When the unsigned int type is compared with the int type, the int type is converted to the unsigned int type. If int is a negative number, converting it to unsigned Int Is a large positive integer, so it is dangerous to compare it.
If unsigned int is forcibly converted to int before comparison, it cannot be said that there is no problem. I think there may also be problems, which is relatively good. (From the strlen return value issue)
Bytes -------------------------------------------------------------------------------------------------------------------------
// Example 1
// String a = "ABCD ";
// Comparison result of-1 and A. Length ()
// Code
#include<iostream>#include<string>using namespace std;int main(){string a="abcd";cout<<"a-----"<<a<<endl;cout<<"a.length()-----"<<a.length()<<endl;if( -1 >= a.length() )cout<<"*************"<<endl; return 0;}
Output:
Bytes -------------------------------------------------------------------------------------------------------------------------
// Example 2
// String a = "ABCD ";
// Comparison result of-1 and A. Size ()
// Code
#include<iostream>#include<string>using namespace std;int main(){string a="abcd";cout<<"a----"<<a<<endl;cout<<"a.size()----"<<a.size()<<endl;if(-1>=a.size())cout<<"*************"<<endl; return 0;}
Output:
Bytes -------------------------------------------------------------------------------------------------------------------------
// Example 3
// Char a [100] = "ABCD ";
// Comparison result of-1 and strlen ()
// Code
#include<iostream>#include<string>using namespace std;int main(){char a[100]="abcd";cout<<"a----"<<a<<endl;cout<<"strlen(a)----"<<strlen(a)<<endl;if( -1>=strlen(a) )cout<<"*************"<<endl; return 0;}
Output:
A ---- ABCD
Strlen (a) ---- 4
*************
Press any key to continue