char类型就直接比较ASCLL值。 字符串类型比较大小: 1:首先比较字符串中的第一个字符的ASCLL值。 2:如果第一个字符相同,则比较第二个字符仍相同,则比较第三……比较第N个字符,直至有不相同。 3:如果字符串长度不等,如(James和Jan)作比较,也取决于ASCLL值,两个字符串的前面两个字母都相同,则比较第三个,因为n的ASCLL值比m的大,所以Jan>James; 4:如果两个字符串比较到末尾还没出现不匹配,则比较短字符串被认为较小。
下面介绍三种字符串比较大小的方法:
1. 直接用比较运算符进行比较(>,<,=)
2. compare函数的使用
#include
using namespace std;
int main(){
string str1="hello";
cout< cout< cout< } 3. 使用strcmp #include #include using namespace std; int main(){ char* str1="hello"; char* str2="hell"; char *str3="helloo"; char *str4="hello"; //原型extern int strcmp(const char *s1,const char *s2); cout< cout< cout< } 以上内容参考网友回答