📜  C++程序使用运算符重载比较两个字符串

📅  最后修改于: 2021-05-30 10:03:58             🧑  作者: Mango

先决条件: C++中的运算符重载

给定两个字符串,如何使用运算符重载检查两个字符串是否相等。

例子:

Input: ABCD, XYZ
Output: ABCD is not equal to XYZ
        ABCD is greater than XYZ

Input: Geeks, Geeks
Output: Geeks is equal to Geeks

方法:使用二进制运算符重载。

  • 声明一个带有字符串变量的类,并使用运算符函数’==’,'<=’和’> =’接受该类的实例,并将其变量与当前实例的字符串变量进行比较。
  • 创建该类的两个实例,并分别使用两个输入字符串初始化其类变量。
  • 现在,使用重载运算符(==,<=和> =)函数比较两个实例的类变量。

下面是上述方法的实现:

// C++ program to compare two Strings
// using Operator Overloading
  
#include 
#include 
#include 
  
using namespace std;
  
// Class to implement operator overloading
// function for concatenating the strings
class CompareString {
  
public:
    // Classes object of string
    char str[25];
  
    // Parametrized Constructor
    CompareString(char str1[])
    {
        // Initialize the string to class object
        strcpy(this->str, str1);
    }
  
    // Overloading '==' under a function
    // which returns integer 1/true
    // if left operand string
    // and right operand string are equal.
    //(else return 0/false)
    int operator==(CompareString s2)
    {
        if (strcmp(str, s2.str) == 0)
            return 1;
        else
            return 0;
    }
  
    // Overloading '<=' under a function
    // which returns integer 1/true
    // if left operand string is smaller than
    // or equal to the right operand string.
    // (else return 0/false)
    int operator<=(CompareString s3)
    {
        if (strlen(str) <= strlen(s3.str))
            return 1;
        else
            return 0;
    }
  
    // Overloading '>=' under a function
    // which returns integer 1/true
    // if left operand string is larger than
    // or equal to the right operand string.
    //(else return 0/false)
    int operator>=(CompareString s3)
    {
        if (strlen(str) >= strlen(s3.str))
            return 1;
        else
            return 0;
    }
};
  
void compare(CompareString s1, CompareString s2)
{
  
    if (s1 == s2)
        cout << s1.str << " is equal to "
             << s2.str << endl;
    else {
        cout << s1.str << " is not equal to "
             << s2.str << endl;
        if (s1 >= s2)
            cout << s1.str << " is greater than "
                 << s2.str << endl;
        else
            cout << s2.str << " is greater than "
                 << s1.str << endl;
    }
}
  
// Testcase1
void testcase1()
{
    // Declaring two strings
    char str1[] = "Geeks";
    char str2[] = "ForGeeks";
  
    // Declaring and initializing the class
    // with above two strings
    CompareString s1(str1);
    CompareString s2(str2);
  
    cout << "Comparing \"" << s1.str << "\" and \""
         << s2.str << "\"" << endl;
  
    compare(s1, s2);
}
  
// Testcase2
void testcase2()
{
    // Declaring two strings
    char str1[] = "Geeks";
    char str2[] = "Geeks";
  
    // Declaring and initializing the class
    // with above two strings
    CompareString s1(str1);
    CompareString s2(str2);
  
    cout << "\n\nComparing \"" << s1.str << "\" and \""
         << s2.str << "\"" << endl;
  
    compare(s1, s2);
}
  
// Driver code
int main()
{
    testcase1();
    testcase2();
  
    return 0;
}
输出:
Comparing "Geeks" and "ForGeeks"
Geeks is not equal to ForGeeks
ForGeeks is greater than Geeks


Comparing "Geeks" and "Geeks"
Geeks is equal to Geeks
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”