![f18923383145395cb2274819c2ddcc47.png](https://img-blog.csdnimg.cn/img_convert/f18923383145395cb2274819c2ddcc47.png)
1 strcmp(s1, s2)
在 C 语言中,我们可以使用 strcmp()
函数比较 char*
类型字符串的字典序。
- 当字符串 s1 的字典序小于字符串 s2 的字典序时,返回值 < 0。
- 当字符串 s2 的字典序大于字符串 s1 的字典序时,返回值 > 0。
- 当字符串 s1 的字典序等于字符串 s2 的字典序时,返回值 = 0。
#include <iostream>
using namespace std;
int main() {
char s1[2] = "a";
char s2[2] = "b";
cout << strcmp(s1, s2) << endl; //-1
cout << strcmp(s2, s1) << endl; //1
cout << strcmp(s1, s1) << endl; //0
return 0;
}
2 s1.compare(s2)
在 C++ 中,我们可以使用 compare()
函数比较 char*
类型和 string
类型字