Boost logo

Boost Users :

From: Pavol Droba (droba_at_[hidden])
Date: 2007-02-21 02:15:39


Meryl Silverburgh wrote:

<snip>

>
> I guess a bigger problem for me for using lexicographical_compare is
> I will get opposite result depends on the order of the input string:
> For example, if I have
> string s1("/1/9"), string s2("/1/9/3");
>
> the compare will return true,
>
> but if I have
>
> string s1("/1/9/3"), string s2("/1/9");
>
> it will return false.
>
> But for my case, I always want "/1/9" less than "/1/9/3"
>
> I guess I can compare the length of the 2 string and always put s1 to
> be the shorter one, and s2 to be the longer one.
>
> if there is an more efficient/cleaner idea, please let me know.
>

Here is an excert from the lexicographica_compare documentation in msdn:

<quote>
A lexicographical comparison between sequences compares them element by
element until:

It finds two corresponding elements unequal, and the result of their
comparison is taken as the result of the comparison between sequences.

No inequalities are found, but one sequence has more elements than the
other, and the shorter sequence is considered less than the longer
sequence.

No inequalities are found and the sequences have the same number of
elements, and so the sequences are equal and the result of the
comparison is false.
</quote>

So the lexicographical_compare does indeed take the lenght of sequences
into account. The problem you see, is due to the nature it handles you
the result.

If lexc(a,b) returns true, you know that a<b,
but if lexc(b,a) returns false, you get weaker condition a<=b.

If this is not sufficient for you, I would suggest to reimplement
lexicographical_compare to return more detailed result (for example
-1,0,1). Look into the sources of lexicographical_compare in boost and
in your standard library. The implementation should be quite straighforward.

Regards,
Pavol.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net