功能描述:

    • 字符串之间的比较

    比较方式:

    • 字符串比较是按照ASCII码进行对比

    = 返回 0
    > 返回 1
    < 返回 -1

    注:字符串对比主要是用于比较两者是否相等

    函数原型:

    1. int compare(const string& s) const; //与字符串s比较
    2. int compare(const char *s) const; //与字符串s比较
    1. // 字符串比较
    2. void test01()
    3. {
    4. string str1 = "xhello";
    5. string str2 = "hello";
    6. int res = str1.compare(str2);
    7. if (res == 0)
    8. {
    9. cout << "str1 == str2" << endl;
    10. }
    11. else if (res > 0)
    12. {
    13. cout << "str1 > str2" << endl;
    14. }
    15. else
    16. {
    17. cout << "str1 < str2" << endl;
    18. }
    19. }
    20. int main(void)
    21. {
    22. test01();
    23. system("pause");
    24. return 0;
    25. }