The std::string::compare() function in C++ is used to perform lexicographical comparison between two strings or substrings. It is a member function of the std::string class defined in the <string> header file.
- It compares strings character by character using ASCII values.
- It can compare entire strings as well as specific portions of strings.
Return Value of compare()
The compare() function returns:
- 0 if both strings (or substrings) are equal.
- A positive value if the first mismatching character in the first string has a greater ASCII value.
- A negative value if the first mismatching character in the first string has a smaller ASCII value.
Overloads of string::compare()
Compare Two Strings
The string::compare() method can be used to compare the one string with another string that is passed to it as argument.
Syntax
str1.compare(str2);
Parameters: str2 - String to compare with str1.
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1("Geeks");
string str2("Geeks");
// Comparing str1 and str2
if ((str1.compare(str2)) == 0)
cout << "String Matched" << endl;
else
cout << "String Not Matched" << endl;
return 0;
}
Output
String Matched
Explanation: The strings "Geeks" and "Geeks" are identical, so compare() returns 0.
Compare Substring with Another String
The string::compare() method also supports the comparison between a part of the string with another string.
Syntax
str1.compare(pos, len, str2);
Parameters
- pos: Starting index in str1.
- len: Number of characters to compare.
- str2: String to compare.
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1("forGeeks");
string str2("Geeks");
// Compares 5 characters starting from index
// 3 of str1 with str2
if ((str1.compare(3, 5, str2)) == 0)
cout << "Substring Matched";
else
cout << "Strings Not Matched";
return 0;
}
Output
Substring Matched
Explanation: The substring of str1 starting at index 3 with length 5 is "Geeks", which matches str2.
Comparing Two Substrings
The compare() function also allows comparison between two substrings.
Syntax
str1.compare(pos1, len1, str2, pos2, len2);
Parameters
- pos1: Starting index in str1.
- len1: Number of characters from str1.
- str2: Second string.
- pos2: Starting index in str2.
- len2: Number of characters from str2.
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1("Geeks");
string str2("forGeeks");
// Compares 5 characters of str1 starting from
// index 0 with 5 characters of str2 starting
// from index 3
if ((str1.compare(0, 5, str2, 3, 5)) == 0)
cout << "Both Substring Matches";
else
cout << "Substrings Not Matched";
return 0;
}
Output
Both Substring Matches
Explanation: The first 5 characters of str1 ("Geeks") are compared with the 5 characters of str2 starting at index 3 ("Geeks"). Since both substrings are equal, compare() returns 0.
Advantages of std::string::compare()
The compare() function provides a convenient way to perform lexicographical comparisons.
- Supports complete string as well as substring comparison.
- Returns ordering information in addition to equality.
- Performs comparisons efficiently without creating temporary strings.
- Provides clearer intent than manual character-by-character comparison.