为了避免 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/