比较字符串时应使用显式的字符串比较函数,例如strcmp()或strncmp()。示例代码如下:
char str1[] = "hello"; char str2[] = "world"; // 错误写法:if (str1 == "hello") {...} // 正确写法: if (strcmp(str1, "hello") == 0) {...} // 字符串相等 if (strncmp(str1, str2, 3) == 0) {...} // 只比较前3个字符是否相等
上一篇:比较结构体时使用==运算符,如何选择参数类型(非const引用vsconst引用)?
下一篇:比较解决方案的运行时间