char与string类型如何进行比较

img

img

把char数组转换成string再比较啊
也可以把string转换为char*,用strcmp比较
https://blog.csdn.net/mpp_king/article/details/88758019

字符串比较用 strcmp(str1, str2) == 0 判断相等的,==是比较指针所指的地址是否相等。

char str[] = "Hello World";
char* p1 = str;
char* p2 = str;
 
bool b1 = (p1 == p2); // true 
bool b2 = (p1 == "Hello World"); // false,注:理论上是false,如果编辑器有优化,将相同的静态字串都指向同一个内存,则为true,但不能依赖于此
bool b3 = (strcmp(p1, "Hello World") == 0); // true
bool b4 = strcmp(p1, "Hello World"); // false
// strcmp如果相等返回0,不相等返回非0,小于为负,大于为正,其实就是两个字符串的差值,所以比较相等时务必写上==0