Boost logo

Boost Users :

From: Meryl Silverburgh (silverburgh.meryl_at_[hidden])
Date: 2007-02-21 00:24:03


On 2/20/07, Pavol Droba <droba_at_[hidden]> wrote:
> Meryl Silverburgh wrote:
> > On 2/20/07, Pavol Droba <droba_at_[hidden]> wrote:
> >>> Thanks, I did try just doing this
> >>> cout << copy_range<std::string>(a) << endl;
> >>> cout << copy_range<std::string>(b) << endl;
> >>>
> >>> But I find out that 'integer_compare' just call once, and it is an empty string.
> >>>
> >>> My input strings were "/1/11/2", "1/9/2". From your previous message,
> >>> I understand the first time return an empty string, but I do expect
> >>> integer_compare will call again and again like "1", "11", "2", "1",
> >>> "9" ,"2"....
> >>> But it did not happens.
> >>>
> >>>
> >> Great, we are getting to the finish ;) There is just one slight problem,
> >> that I have overlooked in your code.
> >>
> >> lexicographical_compare simply forwards the call to
> >> std::lexicographical_compare
> >> http://www.sgi.com/tech/stl/lexicographical_compare.html
> >>
> >> So the given predicate (integer_compare) functions as *isless*. It
> >> means, that it should return true if the first argument is *less* then
> >> the second one. In case of equality it should return false.
> >>
> >> In your case, lexicographical_compare stopped on the first tokens, since
> >> the predicate reported that one is less then other. No other comparison
> >> was necessary to get the result.
> >>
> >> Try to change the result to false. You should be seeing the output from
> >> whole inputs.
> >>
> >> Best regards,
> >> Pavol.
> >
> > Pavol,
> >
> > Thank you very much for your effort/time/help.
> > Yes, I do see the output. But still one question, why my function
> > integer_compare is being call 2 times for every sub-string?
> >
> > my input strings are string s1("/1/11/2"), string s2("/1/9/3");
> >
> > Here is my output:
> >
> > calling integer_compare
> > ()
> > ()
> > calling integer_compare
> > ()
> > ()
> > calling integer_compare
> > (1)
> > (1)
> > calling integer_compare
> > (1)
> > (1)
> > calling integer_compare
> > (11)
> > (9)
> > calling integer_compare
> > (9)
> > (11)
> > calling integer_compare
> > (2)
> > (3)
> > calling integer_compare
> > (3)
> > (2)
> >
>
> If you look better, you see that the arguments are reversed in the
> second call. because the interger_compare implements only '<' operation,
> we need actualy to call it twise to get ==.
>
> Anyway, if you find lexicographical_compare insufficient in some
> regards, I'm sure you can now implement version that suits your needs.
>

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.

> Regards,
> Pavol.
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


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