Boost logo

Boost Users :

From: David Brownell (david_brownell_at_[hidden])
Date: 2006-12-28 02:57:51


When comparing the results of the find method to the end iterator in the
code below, it appears that find is finding elements that are not in the
container. Am I using multi-index in the wrong way?

Thanks for the help!

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/shared_ptr.hpp>

#include <iostream>

struct MyObject : private boost::noncopyable
{
    explicit MyObject(int i) : _i(i) {}
    int Get(void) const { return(_i); }

    bool operator <(MyObject const &rhs) const
    {
        return(_i < rhs._i);
    }

private:
    int _i;
};

typedef boost::shared_ptr<MyObject> MyObjectPtr;

using namespace boost::multi_index;

typedef multi_index_container<MyObjectPtr,
                              indexed_by<sequenced<>,
                                         ordered_unique<identity<MyObject> >
>
> MyContainer;

struct CompareFunc
{
    bool operator()(MyObject const &o, int i) const { return(i == o.Get());
}
    bool operator()(int i, MyObject const &o) const { return(o.Get() == i);
}
};

int main(void)
{
    MyContainer c;
    
    c.push_back(boost::shared_ptr<MyObject>(new MyObject(3)));
    c.push_back(boost::shared_ptr<MyObject>(new MyObject(4)));
    c.push_back(boost::shared_ptr<MyObject>(new MyObject(1)));

    MyContainer::nth_index<1>::type const & r = c.get<1>();
    
    std::cout << (r.find(1, CompareFunc()) == r.end() ? "Not found" :
"found") << "\n";
    std::cout << (r.find(13, CompareFunc()) == r.end() ? "Not found" :
"found") << "\n"; // This should not be found

    return(0);
}


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