Hello,
Compose_key implements lexicographical comparison where (1, true) < (1.5, false) < (2, true).
What you need is a composition of two indices rather than of keys I think, the first filters out elements having proper first keys and the second narrows that by the second key.
HTH
József Mihalicza
-----Original Message-----
From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Oliver.Kowalke@qimonda.com
Sent: Monday, November 27, 2006 2:41 PM
To: boost-users@lists.boost.org
Subject: [Boost-users] [multiindex] - lower_bound/upper_bound return wrongresults
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
#include
#include
#include
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;
}
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users