c++ - boost::multi_index 用户定义的键提取器和复合键

标签 c++ boost multi-index

为了避免 msvc2010 编译器错误,我在 composite_key 中使用用户定义的 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 的最后一个参数是一个自定义 key 提取器,我不知道用什么替换“Node::TypeOne”。 (在我的 equal_range 电话中)

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

最佳答案

使用以下复合键提取器:

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

关于c++ - boost::multi_index 用户定义的键提取器和复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4466574/

相关文章:

C++分配和析构函数

c++ - 每次继承时都需要boost::serialization::base_object吗?

python - 使用 iloc 进行列选择,包括单独的索引和范围

python - 维护 Pandas 多索引数据帧的顺序

C++——为什么我们应该使用运算符 -> 来访问 SmartPtr 的成员函数?

c++ - 帧缓冲对象错误?

c++ - 在 lambda 中使用模板参数

ubuntu - 在 Ubuntu : make can't find Boost's include files 上构建 Caffe

android - Android 的构建 boost

python - Pandas :在多索引数据框中转换索引类型