Question

I have a multi_index_container with an index that is a composite_key. But I can not find a way to erase an element by its key. Please see below:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/composite_key.hpp>

using namespace boost::multi_index;

struct Point
{
    int x, y;
};

void func()
{
  multi_index_container<Point,indexed_by<
    hashed_unique<
      composite_key<Point,
                    member<Point,int,&Point::x>,
                    member<Point,int,&Point::y> >
      > > > points;

  points.find( boost::make_tuple( 3, 3 ) );    // <- works
  points.erase( boost::make_tuple( 3, 3 ) );   // <- failes to compile
}

erase(key) works for indices that are not composite. But I am unable to find the correct syntax for composite keys.

Était-ce utile?

La solution

erasedoesn't have the type of overloads that allow for interoperation with tuples (technically, this relates to the concept of compatible extensions.) But you can have the same effect with a little more code:

auto p=points.equal_range(boost::make_tuple(3,3));
points.erase(p.first,p.second);

Autres conseils

Adding to the previous answer as per your request. You can have it like this:

Point p={3,3};
points.erase(points.key_extractor()(p));

The only problem with this is that it doesn't scale (if Point is expensive to construct.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top