Reputation: 59
Code below when I check if K or Y is greater, what method is used to compare two different strings? number of bits?
#include <iostream>
#include <string>
using namespace std;
int main() {
string y = "can't";
string k = "solve";
if(k > y){
cout << "k is bigger";
}else {
cout << "y is bigger";
}
return 0;
}
k is bigger
Upvotes: 2
Views: 100
Reputation: 7374
string compare is a lexigraphical comparison:
All comparisons are done via the compare() member function (which itself is defined in terms of Traits::compare()):
Two strings are equal if both the size of lhs and rhs are equal and each character in lhs has equivalent character in rhs at the same position.
The ordering comparisons are done lexicographically -- the comparison is performed by a function equivalent to std::lexicographical_compare.
And this is how lexigraphical comparison works:
A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries; It involves comparing sequentially the elements that have the same position in both ranges against each other until one element is not equivalent to the other.
Upvotes: 4
Reputation: 59
These relational operators are overloaded in the header file string. All the relational operators used for string operations can be found in the link below.
http://www.cplusplus.com/reference/string/string/operators/
Hope this clears your doubt.
Upvotes: 0