Topic: <Strings> :: compare


Author: James Kuyper <kuyper@wizard.net>
Date: 1997/09/05
Raw View
Jarkko Kvykkd wrote:
 >
 > Hi! I must resend this information, since I haven't got an answer or I
 > may
 > have missed it.
 >
 > This is an extract from a standard (or draft), can anyone recognize it?
 > I just don't remember which one this is, or where this came from :)
 >
 > The actual message follows:
 >
 > _________________________________________________________
 > 21   Strings library                                     [lib.strings]
 > _________________________________________________________
 >
 > Consider the following string compare: Is it really true, that compare
 > returns < 0 only if ONE element of p is less than the corresponding
 > element
 > of q, while the others are equal to each other?

No there can be many differences; only the first one found affects the
return value. It returns a negative value if the first element of p that
is different is less than the corresponding element of q, and a positive
value if it is greater; the remaining elements are not compared.

 > _________________________________________________________
 > expression   return type        assertion/note
 > complexity
 >                                            pre/post-condition
 > _________________________________________________________
 > X::compare  int      yields: 0 if for each i in [0,n),
 > linear
 >    (p,q,n)                X::eq(p[i],q[i]) is true; else, a nega-
 >                              tive value if, for some j in [0,n),
 >                              X::lt(p[j],q[j]) is true and for each i
 >                              in [0,j) X::eq(p[i],q[i]) is true; else a
 >                              positive value.

Note that the X::eq() check is restricted to the range [0,j), not [0,n)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Jarkko K\vykk\d" <jarkko.koykka@nmp.nokia.com>
Date: 1997/07/16
Raw View

_________________________________________________________
21   Strings library                                     [lib.strings]
_________________________________________________________

Consider the following string compare: Is it really true, that compare
returns < 0 only if ONE element of p is less than the corresponding
element
of q, while the others are equal to each other?
_________________________________________________________
expression   return type        assertion/note
complexity
                                           pre/post-condition
_________________________________________________________
X::compare  int      yields: 0 if for each i in [0,n),
linear
   (p,q,n)                X::eq(p[i],q[i]) is true; else, a nega-
                             tive value if, for some j in [0,n),
                             X::lt(p[j],q[j]) is true and for each i
                             in [0,j) X::eq(p[i],q[i]) is true; else a
                             positive value.


Jarkko
__________________________________________________________________
Jarkko K=F6ykk=E4
Nokia Mobile Phones          Tel. (gsm) +358-50-5816916
Wireless Data                Fax.  +358-10-5056733
P.O. Box 68 (Sinitaival 5)   Fax. (mobile) +358-50-8816916
FIN-33721 Tampere FINLAND    EMail: jarkko.koykka@nmp.nokia.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: David Vandevoorde <daveed@vandevoorde.com>
Date: 1997/07/17
Raw View
Jarkko Kvykkd wrote:
> Consider the following string compare: Is it really true, that compare
> returns < 0 only if ONE element of p is less than the corresponding
> element of q, while the others are equal to each other?

... while the _preceeding_ are equal; the succeeding don't
matter. I believe this is sort of the classic working
definition for lexicographical orderings.

 Daveed
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jim.hyslop@leitch.com (Jim Hyslop)
Date: 1997/07/18
Raw View
In article <33CCD389.DFFC3F77@nmp.nokia.com> on 16 Jul 97 15:08:40
GMT, "Jarkko K\vykk\d" <jarkko.koykka@nmp.nokia.com> says...
>
> _________________________________________________________
> 21   Strings library                                     [lib.strings]
> _________________________________________________________
>
> Consider the following string compare: Is it really true, that compare
> returns < 0 only if ONE element of p is less than the corresponding
> element
> of q, while the others are equal to each other?
Well, yeah, isn't that what you'd expect?  If all corresponding
elements are equal except for one (which is less), then shouldn't it
return negative?  For example:
p: "123056789"
q: "123456789"
       ^

All elements are equal, except the fourth; p[3] < q[3] therefore the
function should return a negative value.

Now, you may be thinking "what if another element of q is greater than
p?" Well, that would depend where that element is:

Consider:
p: "123056789"
q: "123456089"
          ^

In this situation, compare still yields a negative value - for a j of
3, for each i in [0,j) X::eq(p[i],q[i]) is true.

Consider:
p: "123056789"
q: "183456789"
     ^

In this situation, compare will yield a positive value, because for a
j of 3 "for each i in [0,j) X::eq(p[i],q[i])" is not true - p[1] !=
q[1].

In other words, compare works the way you'd expect it to - exactly
like strcmp.

--
Jim Hyslop O-

Imagination is more important than knowledge.
   -- Albert Einstein

Don't pass on that email about someone dying of cancer or a "new
super-powerful computer virus" until you check out the Internet Hoax
page http://ciac.llnl.gov/ciac/CIACHoaxes.html first!
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]