boost :: multi_index用户定义的密钥提取器和组合密钥

时间:2010-12-17 00:02:18

标签: c++ boost multi-index

为了避免msvc2010编译器错误,我在composite_key中使用了一个用户定义的密钥提取器:

enum NodeType
{
     TypeOne = 0,
     TypeTwo
};

struct TypeExtractor
{
 typedef NodeType result_type;

 const result_type& operator()(const boost::shared_ptr<Node>& p) const
 {
  return p->getNodeType();
 }        
};

struct byValueAndType{};

typedef boost::multi_index_container<
 boost::shared_ptr<Node>,
 boost::multi_index::indexed_by<
  boost::multi_index::random_access<>,
  boost::multi_index::ordered_non_unique<
   boost::multi_index::tag<byValueAndType>,
   boost::multi_index::composite_key<
    Node,
    boost::multi_index::const_mem_fun<Node, const std::string&, &Node::getValue>,
    TypeExtractor
   >
  >
 >
> NodeList;

typedef NodeList::nth_index<1>::type NodeListByValueAndType;

这似乎编译得很好(并且vc编译器不再崩溃),但是当我尝试调用equal_range时遇到一些问题:

std::pair<Node::NodeListByValueAndType::const_iterator, Node::NodeListByValueAndType::const_iterator> range;

range = _listNode.get<byValueAndType>().equal_range(boost::make_tuple("MyVal", Node::TypeOne));

在我的旧实现中没问题,因为我的composite_key是'由'两个const_mem_fun组成的。现在,composite_key的最后一个参数是一个自定义键提取器,我不知道用什么来替换'Node :: TypeOne'。 (在我的平等呼叫中)

编译器说他期待一种类型的const boost::shared_ptr<Node>&,但我真的不想创建一个指向良好类型的随机节点的共享指针,仅用于equal_range ...(并且它没有'无论如何都要工作)

1 个答案:

答案 0 :(得分:2)

使用以下复合键提取器:

boost::multi_index::composite_key<
    boost::shared_ptr<Node>,
    boost::multi_index::const_mem_fun<Node, const std::string&, &Node::getValue>,
    TypeExtractor
>