On 2/18/07, Pavol Droba
Hi,
Meryl Silverburgh wrote:
Thanks for your idea.
I have cases like this:
/1/1 /1/2/3/2
Since the first string /1/1 is smaller than the /1/2 (1 is < 2) of the /1/2/3/2, I should able to stop comparison by looking at the 4th character (/1/2) of '/1/2/3/2'.
So for cases like that, I would like to stop as soon as i can tell one string is shorter than the other, instead of splitting the whole string.
This is exactly what would happen you use the second approach I gave you. Constructing the iterator_range from the split iterator does not perform any actual searching, and lexicographical_compare stops on the first possible index (i.e on the first position where inputs are different).
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(r1, r2, integer_compare)) { // s1 is less }
Regards, Pavol
Pavol,
Thanks for your help. I am trying your second approach. But I am
having compilation error in this line:
iterator_range
, string_split)' /usr/include/boost/algorithm/string/iterator_range.hpp:89: note: candidates are: boost::algorithm::iterator_range<IteratorT>::iterator_range(const boost::algorithm::iterator_range<IteratorT>&) [with IteratorT = string_split] /usr/include/boost/algorithm/string/iterator_range.hpp:85: note:
Complete program:
#include <iostream>
#include <string>
#include "boost/algorithm/string/find_iterator.hpp"
#include "boost/algorithm/string/classification.hpp"
using namespace std;
using namespace boost::algorithm;
typedef split_iteratorstring::const_iterator string_split;
int main(int argc, char **argv) {
cout << "hello world" << endl;
string s1("/1/1/2");
string s2("/1/1/3");
iterator_range
, string_split)' /usr/include/boost/algorithm/string/iterator_range.hpp:89: note: candidates are: boost::algorithm::iterator_range<IteratorT>::iterator_range(const boost::algorithm::iterator_range<IteratorT>&) [with IteratorT = string_split] /usr/include/boost/algorithm/string/iterator_range.hpp:85: note: boost::algorithm::iterator_range<IteratorT>::iterator_range(const std::pair<_ForwardIterator, _ForwardIterator>&) [with IteratorT = string_split] /usr/include/boost/algorithm/string/iterator_range.hpp:81: note: boost::algorithm::iterator_range<IteratorT>::iterator_range(IteratorT, IteratorT) [with IteratorT = string_split] /usr/include/boost/algorithm/string/iterator_range.hpp:77: note: boost::algorithm::iterator_range<IteratorT>::iterator_range() [with IteratorT = string_split] ../test.cpp:21: error: no matching function for call to `boost::algorithm::iterator_range
::iterator_range(boost::algorithm::split_iterator<__gnu_cxx::__normal_iterator , string_split)' /usr/include/boost/algorithm/string/iterator_range.hpp:89: note: candidates are: boost::algorithm::iterator_range<IteratorT>::iterator_range(const boost::algorithm::iterator_range<IteratorT>&) [with IteratorT = string_split] /usr/include/boost/algorithm/string/iterator_range.hpp:85: note: boost::algorithm::iterator_range<IteratorT>::iterator_range(const std::pair<_ForwardIterator, _ForwardIterator>&) [with IteratorT = string_split] /usr/include/boost/algorithm/string/iterator_range.hpp:81: note: boost::algorithm::iterator_range<IteratorT>::iterator_range(IteratorT, IteratorT) [with IteratorT = string_split] /usr/include/boost/algorithm/string/iterator_range.hpp:77: note: boost::algorithm::iterator_range<IteratorT>::iterator_range() [with IteratorT = string_split] ../test.cpp:23: error: `integer_compare' was not declared in this scope ../test.cpp:23: warning: unused variable 'integer_compare' ../test.cpp: At global scope: ../test.cpp:31: error: expected declaration before '}' token /usr/include/boost/function/function_template.hpp: In static member function `static R boost::detail::function::function_obj_invoker2 ::invoke(boost::detail::function::any_pointer, T0, T1) [with FunctionObj = boost::algorithm::detail::is_any_ofF<char>, R = boost::algorithm::iterator_range<__gnu_cxx::__normal_iterator , T0 = __gnu_cxx::__normal_iterator >, T1 = __gnu_cxx::__normal_iterator >]': /usr/include/boost/function/function_template.hpp:482: instantiated from `void boost::function2 ::assign_to(FunctionObj, boost::detail::function::function_obj_tag) [with FunctionObj = boost::algorithm::detail::is_any_ofF<char>, R = boost::algorithm::iterator_range<__gnu_cxx::__normal_iterator , T0 = __gnu_cxx::__normal_iterator >, T1 = __gnu_cxx::__normal_iterator >, Allocator = std::allocatorboost::function_base]' /usr/include/boost/function/function_template.hpp:433: instantiated from `void boost::function2 ::assign_to(Functor) [with Functor = boost::algorithm::detail::is_any_ofF<char>, R = boost::algorithm::iterator_range<__gnu_cxx::__normal_iterator , T0 = __gnu_cxx::__normal_iterator >, T1 = __gnu_cxx::__normal_iterator >, Allocator = std::allocatorboost::function_base]' /usr/include/boost/function/function_template.hpp:289: instantiated from `boost::function2 ::function2(Functor, typename boost::enable_if_c< boost::type_traits::ice_not< boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::algorithm::detail::is_any_ofF<char>, R = boost::algorithm::iterator_range<__gnu_cxx::__normal_iterator , T0 = __gnu_cxx::__normal_iterator >, T1 = __gnu_cxx::__normal_iterator >, Allocator = std::allocatorboost::function_base]' /usr/include/boost/algorithm/string/detail/find_iterator.hpp:50: instantiated from `boost::algorithm::detail::find_iterator_base<IteratorT>::find_iterator_base(FinderT, int) [with FinderT = boost::algorithm::detail::is_any_ofF<char>, IteratorT = __gnu_cxx::__normal_iterator ]' /usr/include/boost/algorithm/string/find_iterator.hpp:262: instantiated from `boost::algorithm::split_iterator<IteratorT>::split_iterator(IteratorT, IteratorT, FinderT) [with FinderT = boost::algorithm::detail::is_any_ofF<char>, IteratorT = __gnu_cxx::__normal_iterator >]' /usr/include/boost/algorithm/string/find_iterator.hpp:351: instantiated from `boost::algorithm::split_iterator boost::algorithm::make_split_iterator(CollectionT&, FinderT) [with CollectionT = std::string, FinderT = boost::algorithm::detail::is_any_ofF<char>]' ../test.cpp:18: instantiated from here /usr/include/boost/function/function_template.hpp:111: error: no match for call to `(boost::algorithm::detail::is_any_ofF<char>) (__gnu_cxx::__normal_iterator >&, __gnu_cxx::__normal_iterator >&)' make: *** [test.o] Error 1 make: Target `all' not remade because of errors. Build complete for project str
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users