jonnyd42
jonnyd42

Reputation: 500

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?

Upvotes: 3

Views: 2334

Answers (2)

dfrib
dfrib

Reputation: 73196

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.

Upvotes: 3

vacuumhead
vacuumhead

Reputation: 519

Length is taken into account, but not in the way you expect. In string comparison, the first characters of each string are compared to each other first. If they are equal, then the second characters are compared and so on. So in your example, the first characters to be compared are '1' and '8'. '8' is larger.

If you had compared "10.72" against "1.87", the first characters would be equal, so the next thing would be to compare "0" against ".".

If you want to compare numeric values, you have to convert the strings to their numeric representation, or else you would have to write your own comparator that would treat strings as numerics. I hope that sheds some light on it.

Upvotes: 0

Related Questions