string::compare() problem?

Gabriel Dos Reis gdr@integrable-solutions.net
Sun Mar 21 17:21:00 GMT 2004


Paolo Carlini <pcarlini@suse.de> writes:

| Hi,
| 
| first, the current behavior is correct: strings are compared
| "lexicographically":
| ever wondered why in your dictionary 'express' comes before
| 'expressionism'? ;)

I think the original poster was expecting a yes/no or true/false or
1/0 answer from string::compare().  I think we can assume that he is
well aware that, in reasonable English dictionaries, "express" comes
before "expressionism" and the reason why. 

To the attention of the original poster:

Whether standard strings are compared lexicographically or by multiset
ordering or by apocope ordering or by epenthesis ordering is
irrelevant for the use of the return value of string::compare().

Since that return value is an int, users should expect any value
in the range INT_MIN..INT_MAX could be returned and should not depend
on the _particular_ returned value.  Instead, users should use only
the relative position of that value with respect to 0:

   The expression lhs.compare(rhs) with

    (1) a value 0 indicates that lhs and rhs are equal;
  
    (2) a value < 0 indicates that lhs is less than rhs;
   
    (3) a value > 0 indicates that rhs is greater than rhs.

-- Gaby



More information about the Libstdc++ mailing list