Comparison Members

Three of the methods in the String class perform comparisons. The operator<() function returns true if the first string comes before the second string alphabetically (or, more precisely, in the machine collating sequence). The simplest way to implement the string comparison functions is to use the standard strcmp() function, which returns a negative value if its first argument precedes the second alphabetically, 0 if the strings are the same, and a positive value if the first follows the second alphabetically. So you can use strcmp() like this:

bool operator<(const String &st1, const String &st2){    if (std::strcmp(st1.str, st2.str) < 0)        return true;    else        return false;}

Because the built-in < operator already ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.