C++ <string> - compare() Function
The C++ string::compare function is used to compare the value of the string object (or a substring) to the sequence of characters specified by the arguments.
Syntax
//1. string int compare (const string& str) const; //2. substring 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; //3. c-string int compare (const char* s) const; int compare (size_t pos, size_t len, const char* s) const; //4. buffer int compare (size_t pos, size_t len, const char* s, size_t n) const;
//1. string int compare (const string& str) const noexcept; //2. substrings 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; //3. c-string int compare (const char* s) const; int compare (size_t pos, size_t len, const char* s) const; //4. buffer int compare (size_t pos, size_t len, const char* s, size_t n) const;
//1. string int compare (const string& str) const noexcept; //2. substrings 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 = npos) const; //3. c-string int compare (const char* s) const; int compare (size_t pos, size_t len, const char* s) const; //4. buffer int compare (size_t pos, size_t len, const char* s, size_t n) const;
Parameters
str |
Specify another string, used entirely or partially as comparing string. |
pos |
Specify position of the first character in the compared string. If it greater than the string length, it throws out_of_range exception. |
len |
Specify length of the compared string (if the string is shorter, as many characters as possible). |
subpos |
Same as pos above, but for the comparing string. |
sublen |
Same as len above, but for the comparing string. |
s |
Specify pointer to an array of characters. If argument n is specified, the first n characters in the array are used as the comparing string, else a null-terminated sequence is expected. |
n |
Specify number of characters to compare. |
Return Value
Returns the following value:
- 0, if compared string and comparing string are equal.
- less than 0, when either the value of the first character that does not match is lower in the compared string or all compared characters match but the compared string is shorter.
- greater than 0, when either the value of the first character that does not match is greater in the compared string or all compared characters match but the compared string is longer.
Time Complexity
Generally linear i.e, Θ(n).
Example:
In the example below, the string::compare function is used to compare the value of the given string object with the specified sequence of characters.
#include <iostream> #include <string> using namespace std; int main (){ string str1 = "green light"; string str2 = "red light"; //gives negative result as first unmatched character //of str1 is smaller than that of str2 int result1 = str1.compare(str2); cout<<"result1: "<<result1<<endl; //gives positive result as first unmatched character //of str1 subtring is greater than that of str2 int result2 = str1.compare(1, 10, str2); cout<<"result2: "<<result2<<endl; //gives zero as str1 subtring is same as str2 substring int result3 = str1.compare(6, 5, str2, 4, 5); cout<<"result3: "<<result3<<endl; return 0; }
The output of the above code will be:
result1: -11 result2: 1 result3: 0
❮ C++ <string> Library