Boost logo

Boost Users :

From: Oliver.Kowalke_at_[hidden]
Date: 2006-11-27 08:41:07


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 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