Difference between Relational operator(==) and std::string::compare() in C++

Comparing String using Relational operators, Using compare()

Relational operators vs std::string::compare()

  1. Return Value : Relational operators return boolean value, while compare() returns unsigned integer.
  2. Parameters : Relational operators need only two strings to perform comparison, one which is being compared and other one is for reference, while compare() can accept different arguments to perform certain task accordingly.
  3. Comparison Method : Relational operators compare characters lexicographically according to the current character traits, while compare() can process more than one argument for each string so that you can specify a substring by its index and by its length.
  4. Operation : We can perform comparison in a part of string directly, using compare() which is otherwise quite a long process with relational operators.
    Example : * Compare 3 characters from 3rd position of str1 with 3 characters from 4th position of str2.

    * str1 = "GeeksforGeeks"
    * str2 = "HelloWorld!"

    Using compare() :

    
    // CPP code to perform comparison using compare()
    #include <iostream>
    using namespace std;
    
    void usingCompare(string str1, string str2)
    {
        // Direct Comparison
        if (str1.compare(2, 3, str2, 3, 3) == 0)
            cout << "Both are same";
        else
            cout << "Not equal";
    }
    
    // Main function
    int main()
    {
        string s1("GeeksforGeeks");
        string s2("HelloWorld !");
        usingCompare(s1, s2);
    
        return 0;
    }
    

    Output:

    Not equal
    

    Using Relational Operators :

    
    // CPP code for comparison using relational operator
    #include <iostream>
    using namespace std;
    
    void relational_operation(string s1, string s2)
    {
        int i, j;
    
        // Lexicographic comparison
        for (i = 2, j = 3; i <= 5 && j <= 6; i++, j++) {
            if (s1[i] != s2[j])
                break;
        }
        if (i == 6 && j == 7)
            cout << "Equal";
        else
            cout << "Not equal";
    }
    
    // Main function
    int main()
    {
        string s1("GeeksforGeeks");
        string s2("HelloWorld !");
        relational_operation(s1, s2);
    
        return 0;
    }
    

    Output:

    Not equal
    

    We can clearly observe the extra processing we need to go through while using relational operators.

This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.