代码

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. //string字符串的比较
  5. //比较方式:按照ASCll
  6. // = 返回 0
  7. // > 返回 1
  8. // < 返回 -1
  9. //函数原型
  10. /*
  11. ① int compare(const string &s) const //与字符串s比较
  12. ② int compare(const char *s) const //与字符串s比较
  13. */
  14. void test01(){
  15. string s1 = "xello";
  16. string s2 = "hello";
  17. if (s1.compare(s2) == 0) {
  18. cout << "s1 等于 s2" << endl;
  19. }
  20. else if (s1.compare(s2) > 0) {
  21. cout << "s1 大于 s2" << endl;
  22. }
  23. else {
  24. cout << "s1 小于 s2" << endl;
  25. }
  26. }
  27. int main() {
  28. test01();
  29. return 0;
  30. }

字符串的比较主要用于判断两个字符串是否相同。