The Overloaded < Operator

PHOTO EMBED

Thu Apr 15 2021 16:53:13 GMT+0000 (UTC)

Saved by @wowza12341 #c++

std::string test1 = "Baked";
std::string test2 = "Bake";  
bool compare = test1 < test2;
 
// Output: 0
/* Reason: There is no difference until the last letter so then it will compare the last letter with the blank space. 
Since blank space comes first before the letter d, this means that test1 is not less than test2 and that is why it returns false.*/
 
std::string test1 = "Baker";
std::string test2 = "Bdke";  
bool compare = test1 < test2;
 
// Output: 1
/* Reason: There is no difference until the second letter so then it will compare the second letter with the second letter of the other word.
Since the letter a comes first before the letter d, this means that test1 is less than test2 and that is why it returns true.*/
content_copyCOPY

When using the overloaded < operator this means it compares the two words. It will check each letter until it sees a different letter in the word and if it does it will compare the that is different with the letter at the same position. If both words are the same it returns false / 0. Remember: A blank compared with a letter, the blank always comes first.