
- C++ Library - Home
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ STL Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- The C++ Advanced Library
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ Library -
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ String::compare() function
The C++ std::string::compare() function is used to compare two strings. It provides a way to lexicographically compare the content of the string objects with another string or a sub string. It returns a integer that are listed below :
0: The strings are equal
<0: The calling string is less than the argument string.
> 0: The calling string is greater than the argument string.
This function can be used to compare the entire string or a specified range of characters.
Syntax
Following is the syntax for std::string::compare() function.
int compare (const string& str) const noexcept; or int compare (size_t pos, size_t len, const string& str) const; int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const; or int compare (const char* s) const; int compare (size_t pos, size_t len, const char* s) const; or int compare (size_t pos, size_t len, const char* s, size_t n) const;
Parameters
- str − It indicates the another string object.
- len − It indicates the length of the compared string.
- pos − It indicates the position of the first character in the corresponding string.
- subpos, sublen − same as pos and len above.
- n − It indicates the number of characters to compare.
- s − It indicates the pointer to an array of characters.
Return value
It returns a signed integral indicating the relation between the strings.
Exceptions
if an exception is thrown, there are no changes in the string.
Example 1
Following is the basic example to demonstrate the string::compare using C++.
#include#include using namespace std; int main() { string X1 = "apple"; string X2 = "banana"; int result = X1.compare(X2); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1 << " is less than " << X2 << endl; } else if (result > 0) { cout << X1 << " is greater than " << X2 << endl; } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
apple is less than banana
Example 2
In this example, we are comparing one substring with the another string. So it prints the output as equal.
#include#include using namespace std; int main() { string X1 = "Tutorialspoint"; string X2 = "point"; int result = X1.compare(9, 5, X2); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1.substr(6, 5) << " is less than " << X2 << endl; } else if (result > 0) { cout << X1.substr(6, 5) << " is greater than " << X2 << endl; } return 0; }
Output
If we run the above code it will generate the following output.
Both string are equal.
Example 3
Following is an another example to compare substrings of two strings using string::compare() function.
#include#include using namespace std; int main() { string X1 = "hello world"; string X2 = "goodbye world"; int result = X1.compare(6, 5, X2, 8, 5); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1.substr(6, 5) << " is less than " << X2.substr(8, 5) << endl; } else { cout << X1.substr(6, 5) << " is greater than " << X2.substr(8, 5) << endl; } return 0; }
Output
Following is the output of the above code.
The substrings are equal.
Example 4
In this example, we compare with one string with the empty string.
#include#include using namespace std; int main() { string X1 = "hello"; string X2 = ""; int result = X1.compare(X2); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1 << " is lesser than " << X2 << endl; } else { cout << X1 << " is greater than " << X2 << endl; } return 0; }
Output
Following is the output of the above code.
hello is greater than