本文介绍了使用multipository_key从multi_index_container擦除每个键的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个multi_index_container
,其索引为composite_key
.但是我找不到通过其键擦除元素的方法.请参见以下内容:
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)
适用于非复合索引.但是我找不到复合键的正确语法.
erase(key)
works for indices that are not composite. But I am unable to find the correct syntax for composite keys.
推荐答案
erase
没有允许与元组互操作的重载类型(从技术上讲,这与 兼容的扩展名 .)但是您可以达到相同的效果还有更多代码:
erase
doesn'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);
这篇关于使用multipository_key从multi_index_container擦除每个键的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!