Search code examples
c++stringcomparisonoperator-keywordrelational

String comparisons with relational operators (differing lengths)


I was comparing two strings in C++ as follows:

if(s1 <= s2)
  //do stuff

I had forgotten the intricacies of string comparison and quickly learned that in the following case:

s1 = "10.72";
s2 = "8.87";

The statement will evaluate to true and do whatever is inside the conditional. The comparison happens between the 8 and the 1. All ASCII representations of numbers are in increasing order from 48 (0) - 57 (9), and obviously 1 < 8.

I had thought that C++ took into account string length but that's incorrect. Would someone mind explaining why length is not taken into account from a C++ language design perspective?


Solution

  • Length is, in fact, taken into account, implicitly, through lexicographical comparison that is used when you invoke the less than < or less or equal to <= operators on strings.

    • Two ranges are compared element by element.

    • The first mismatching element defines which range is lexicographically less or greater than the other.

    • If one range is a prefix of another, the shorter range is lexicographically less than the other.

    • If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.

    From http://en.cppreference.com/w/cpp/algorithm/lexicographical_compare

    Hence, as an example

    "10.72" < "10.721"    // true
    "10.72" == "10.72"    // true (by string comparison as well as lexicographically equalness)
    "10.7211" < "10.7212" // true
    

    Why, you ask? This is not an intricity of C++, but of how to compare strings, where lexicographical comparison is one of the most common (and in my opinion, most logical) comparison methods.