|
Boost Users : |
From: Mihalicza, Jozsef (JMihalicza_at_[hidden])
Date: 2006-11-27 08:59:28
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_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Oliver.Kowalke_at_[hidden]
Sent: Monday, November 27, 2006 2:41 PM
To: boost-users_at_[hidden]
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 <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;
}
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
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