[Multi-Index] find returning false positives?
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
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); }
Hello David,
----- Mensaje original -----
De: David Brownell
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! [...] 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);} };
Here's the problem: your comparison functor must have less-than rather than equality semantics. Try rewriting it as follows: struct CompareFunc { bool operator()(MyObject const &o, int i) const { return(o.Get() < i);} bool operator()(int i, MyObject const &o) const { return(i < o.Get());} }; Hope this helps. Thank you for using Boost.MultiIndex. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
"JOAQUIN LOPEZ MU?Z"
-
David Brownell