×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

std::string::compare() function with example in C++

C++ STL | std::string::compare() function: Here, we are going to see the details of compare function of standard library and its usage details. It's mainly used to compare strings in C++. Also, there is an extension in usage which helps to compare substrings even which is a big plus.
Submitted by Radib Kar, on July 23, 2020

C++ STL - std::string::compare()

string::compare() is a standard library function that is used to compare between two strings or b/w two substrings as per use cases.

Syntax

int compare (const string& str) const;

The invoking string is the compared string and the string str which is passed in the argument is the compared string.

So, say we have two strings str1 & str2. Then the syntax would be str1.compare(str2) & based on the return value we can infer to the comparison result.

Using the above syntax,

  • str1 - compared string which invokes the function
  • str2 - comparing string which is passed as argument in the function

Now there can be three cases:

Function return 0 str1 == str2
Function return negative number(<0) str1 < str2
Function return positive number(>0) str1 > str2

Sample Input and Output

Say str1="abcd", str2="abc"

Then str1.compare(str2) will return positive integer 
as str1 is greater than str2

Say str1="includehelp", str2="india"
Then str1.compare(str2) will return negative integer 
as str1 is smaller than str2

Say str1="abcd", str2="abcd"
Then str1.compare(str2) will return 0 as both strings 
are equal.

Example

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str1, str2;
 
    cout << "Enter string1:\n";
    cin >> str1;
    cout << "Enter string2:\n";
    cin >> str2;
 
    //str1 comparing string which invokes the function
    //str2 is compared string which is passed in argument
    
    int k = str1.compare(str2);
    
    if (k < 0) {
        cout << str1 << " is smaller, " << str2 << " is greater\n";
    }
    else if (k > 0) {
        cout << str2 << " is smaller " << str1 << " is greater\n";
    }
    else { //k==0
        cout << "Both strings are equal\n";
    }

    return 0;
}

Output

Enter string1:
includehelp
Enter string2:
india
includehelp is smaller, india is greater

Extending the function usage to compare substrings

compare() function can be extended to compare substrings even. For that, we use a different syntax (overloaded version).

Syntax

int compare(
    size_t pos, 
    size_t len, 
    const string& str,
    size_t subpos, 
    size_t sublen) const;

So, we will call like str1.comapre(pos,len,str2,sublen,sublen)

In this above syntax, the invoking string is the compared string whose substring to be compared is defined by pos & len. We can compare it with a substring of str (a comparing string which is passed in argument) defined by subpos & sublen.

  • str1 - invoking string
  • pos - starting index of substring for the invoking string, str1
  • len - length of substring for the invoking string, str1
  • str2 - comparing string passed in argument
  • subpos - starting index of substring for the comparing string, str2
  • sublen - length of substring for the comparing string, str2

Here is an example with sample input and output:

Say,
str1="cricket" & str2=wicket" 
Now,
str1.compare(2, 5, str2, 1, 5) will return 0 
because it's basically comparing between 
str1.substr(2, 5) & str2.subtr(1,5)

str1.subtsr(2, 5)= "icket" & str2.subtsr(1, 5)= "icket" 
which are exactly same and 
thus it returns 0( 0 is returns where the strings are same)

Example

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str1 = "cricket", str2 = "wicket";

    // str1 comparing string which invokes the function
    // str2 is compared string which is passed in argument
    // pos=2, length=5, thus compared subtsring is 
    // str1.substr(2,5)="icket"
    // subpos=1,sublength=5, thus compared subtsring is 
    // str2.substr(1,5)="icket"
    
    int k = str1.compare(2, 5, str2, 1, 5);
    
    if (k < 0) {
        cout << str1.substr(2, 5) << " is smaller, " << str2.substr(1, 5) << " is greater\n";
    }
    else if (k > 0) {
        cout << str2.substr(1, 5) << " is smaller " << str1.substr(2, 5) << " is greater\n";
    }
    else { //k==0
        cout << "Both strings are equal\n";
    }

    return 0;
}

Output

Both strings are equal

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.