|
Boost Users : |
From: Pavol Droba (droba_at_[hidden])
Date: 2007-02-17 17:17:42
Hi,
Meryl Silverburgh wrote:
> Hi,
>
> I need to compare 2 strings in this format (a integer separator by '/':
> /1/1
> /1/1/2
> /1/1/3
>
> so
> "/1/1" is less than "/1/1/2" and "/1/1/2" is less than " "/1/1/3".
>
> Is there anything in Boost string library to help me to write such a comparison?
> Is there a string iterator which literates base on the separator '/'?
> My idea is to find an iterator to loop thru the string based on
> separator '/'.
There is a split_iterator which you can use. And there is a generic
lexicographical_compare function.
I've got an idea that might work. But it is a little elaborate.
First, tokenize your string using split, but store the result to
vector<iterator_range>.
Then define a comparison function, that will take the two
iterator_ranges and compare them. Note, that in this case, tha range
should hold an integer string.
Then simply use lexicographical_compare for the comparison.
vector<iterator_range> vec1;
split(vec, s1);
vector<iterator_range> vec2;
split(vec, s2);
if(lexicographical_compare(vec1, vec2, integer_compare))
{
// s1 is less
}
You can even throw away the vector, and directly create an
iterator_range from the split_iterator. This way you may spare some
unnecessary tokenization.
typedef split_iterator<string::const_iterator> string_split;
iterator_range<string_split> r1(
make_split_iterator(s1, is_any_of("/")), string_split())
iterator_range<string_split> r2(
make_split_iterator(s2, is_any_of("/")), string_split())
if(lexicographical_compare(vec1, vec2, integer_compare))
{
// s1 is less
}
Best 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