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.
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 vec1;
split(vec, s1);
vector 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_iteratorstring::const_iterator string_split;
iterator_range r1(
make_split_iterator(s1, is_any_of("/")), string_split())
iterator_range r2(
make_split_iterator(s2, is_any_of("/")), string_split())
if(lexicographical_compare(vec1, vec2, integer_compare))
{
// s1 is less
}
Best Regards,
Pavol.