C++之string字符串比较方法

📣📣📣📣📣📣📣📣

✏️作者主页:枫霜剑客

📋 系列专栏:C++实战宝典

🌲 上一篇: C++之string字符串不同类型间转换

📣📣📣📣📣📣📣📣

🎍逐梦编程,让中华屹立世界之巅。

🎍简单的事情重复做,重复的事情用心做,用心的事情坚持做;

在这里插入图片描述

文章目录

  • 前言
  • 一、compare()函数原型
  • 二、比较方式:
  • 三、compare()函数应用
  • 四、比较运算符
  • 总结
  • 壁纸

前言

字符串可以和类型相同的字符串相比较,也可以和具有同样字符类型的数组比较。

Basic_string 类模板既提供了 >、=、<=、!= 等比较运算符,还提供了 compare() 函数,其中 compare() 函数支持多参数处理,支持用索引值和长度定位子串进行比较。该函数返回一个整数来表示比较结果。如果相比较的两个子串相同,compare() 函数返回 0,否则返回非零值。


一、compare()函数原型

int compare (const basic_string& s) const;
int compare (const Ch* p) const;
int compare (size_type pos, size_type n, const basic_string& s) const;
int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;

二、比较方式:

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

若 = 返回 0

若 > 返回 1

若 < 返回 -1

三、compare()函数应用

代码如下(示例):

/*-----------------------------------【程序说明】----------------------------
*			 项目命题:   C++之string字符串比较方法
* 			 代码所属:   枫霜剑客
* 			     作者:   阿甘
* 		     开发时间:   2022/9/03
*			IDE 版 本:   Visual Studio 2019
*		     项目版本:   1.0.0.1
*---------------------------------------------------------------------------*/
原文链接:https://blog.csdn.net/gzplyx?type=blog

#include 
#include 
using namespace std;
int main ()
{
    string X ("aBcdef");
    string Y ("AbcdEf");
    string C ("789456");
    string D ("789dfg");
    //下面是各种比较方法
    int a=X.compare (Y); //完整的A和B的比较
    int b=X.compare(1,5,Y,4,2); //"Bcdef"和"AbcdEf"比较
    int p=X.compare(1,5,Y,4,2); //"Bcdef"和"Ef"比较
    int q=C.compare(0,3,D,0,3); //"789"和"789"比较
    cout << "a = " << a << ", b = " << b <<", p = " << p << ", q = " << q << endl;
    cin.get();
    return 0;
}

四、比较运算符

String 类的常见运算符包括 >、=、<=、!=。其意义分别为"大于"、“小于”、“等于”、“大于等于”、“小于等于”、“不等于”。

代码如下(示例):

/*-----------------------------------【程序说明】----------------------------
*			 项目命题:   C++之string字符串比较方法
* 			 代码所属:   枫霜剑客
* 			     作者:   阿甘
* 		     开发时间:   2022/9/03
*			IDE 版 本:   Visual Studio 2019
*		     项目版本:   1.0.0.1
*---------------------------------------------------------------------------*/
//原文链接:https://blog.csdn.net/gzplyx?type=blog

#include 
#include 
using namespace std;
void JudgeString(int ch)
{
    cout << (ch?"True":"False")<<endl;
}

int main ()
{
    string Z = "DEF";
    string CH1 = "ABC";
    string CH2 = "DEF";
    string CH3 = "DEFG";
    string CH4 ="def";
    cout << "Z = " << Z << endl;
    cout << "CH1 = " << CH1 <<endl;
    cout << "CH2 = " << CH2 <<endl;
    cout << "CH3 = " << CH3 <<endl;
    cout << "CH4 = " << CH4 <<endl;
    cout << "Z <= CH1 returned ";
    JudgeString (Z <=CH1);
    cout << "Z <= CH2 returned ";
    JudgeString (Z <= CH2);
    cout << "Z <= CP3 returned ";
    JudgeString (Z <= CH3);
    cout << "CH1 <= Z returned ";
    JudgeString (CH1 <= Z);
    cout << "CH2 <= Z returned ";
    JudgeString (CH2 <= Z);
    cout << "CH4 <= Z returned ";
    JudgeString (CH4 <= Z);
    cin.get();
    return 0;
}

总结

在这里插入图片描述

以上就是今天要讲的内容,使用比较运算符可以非常容易地实现字符串的大小比较。在使用时比较运算符时,请大家注意,对于参加比较的两个字符串,任一个字符串均不能为 NULL,否则程序会异常退出。

大家的「关注❤️ + 点赞👍 + 收藏⭐」就是我创作的最大动力!谢谢大家的支持,我们下文见!

在这里插入图片描述

在这里插入图片描述

壁纸

在这里插入图片描述

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/85657fe501.html