BZ易风

导航

 

比较第一个不相同的字符的大小

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <string>
/*
比较
compare函数在>时返回 1,<时返回 -1,==时返回 0。
比较区分大小写,比较时参考字典顺序,排越前面的越小。
大写的A比小写的a小。

int compare(const string& s) const;//与字符串s比较
int compare(const char* s) const;//与字符串s比较
*/
void test01()
{
    string s1 = "abc";      //比较第一个不相同的字符大小
    string s2 = "ac";
    if (s1.compare(s2) == 0)
    {
        cout << "s1和s2相等" << endl;
    }
    else if (s1.compare(s2) == 1)
    {
        cout << "s1大于s2" << endl;
    }
    else {
        cout << "s1小于s2" << endl;
    }
}

int main()
{
    test01();
    system("Pause");
    return 0;
}

结果:

 

posted on 2021-08-26 11:29  BZ易风  阅读(206)  评论(0编辑  收藏  举报