[multiindex] - lower_bound/upper_bound return wrong results

Hello, The example below should print out only the items {1.0, true} and {2.0, true} but not {1.5, false}. What is going wrong? Regards, Oliver #include <algorithm> #include <iostream> #include <iterator> #include <cstdlib> #include <stdexcept> #include <utility> #include <boost/lambda/lambda.hpp> #include <boost/multi_index_container.hpp> #include <boost/multi_index/key_extractors.hpp> #include <boost/multi_index/ordered_index.hpp> namespace mi = boost::multi_index; namespace ld = boost::lambda; class item { private: double weight_; bool asignable_; public: item( double weight, bool asignable) : weight_( weight), asignable_( asignable) {} double weight() const { return weight_; } bool asignable() const { return asignable_; } }; std::ostream & operator<<( std::ostream & os, item const& i) { os << i.weight() << " " << std::boolalpha << i.asignable(); return os; } int main( int argc, char *argv[]) { try { typedef mi::multi_index_container< item, mi::indexed_by< mi::ordered_unique< mi::composite_key< item, mi::const_mem_fun< item, double, & item::weight >, mi::const_mem_fun< item, bool, & item::asignable > > > > > item_table; item_table s; s.insert( item( 0.1, true) ); s.insert( item( 0.5, true) ); s.insert( item( 1.0, true) ); s.insert( item( 1.5, false) ); s.insert( item( 2.0, true) ); typedef item_table::nth_index< 0 >::type idx_type; idx_type::iterator it0 = s.lower_bound( boost::make_tuple( 0.8, true) ); idx_type::iterator it1 = s.upper_bound( boost::make_tuple( 2.5, true) ); std::copy( it0, it1, std::ostream_iterator< item >( std::cout, "\n") ); /* output: 1 true 1.5 false // wrong! 2 true */ return EXIT_SUCCESS; } catch ( std::exception const& e) { std::cerr << e.what() << std::endl; } catch ( ... ) { std::cerr << "unhandled exception" << std::endl; } return EXIT_FAILURE; }

Oliver.Kowalke@qimonda.com wrote:
Hello, The example below should print out only the items {1.0, true} and {2.0, true} but not {1.5, false}. What is going wrong?
The result is correct. Composite keys do comparison from left to right: only when the weight is equal is the assignability even considered for sorting. Thus, {1.5, false} sorts between {1.0, true} and {2.0, true} and thus is part of your range. Switch the order in your composite key and you'll get the results you expect: mi::composite_key< item, mi::const_mem_fun< item, bool, &item::asignable >, mi::const_mem_fun< item, double, & item::weight > > Sebastian Redl
participants (2)
-
Oliver.Kowalke@qimonda.com
-
Sebastian Redl