如果我有这样一个对象:
struct Bar {
std::string const& property();
};
我可以像这样为它创建一个多索引容器:
struct tag_prop {};
typedef boost::multi_index_container<
Bar,
boost::multi_index::indexed_by<
boost::multi_index::ordered_non_unique<
boost::multi_index::tag<tag_prop>,
boost::multi_index::const_mem_fun<
Bar, const std::string&, &Bar::property
>
>
>
, ... other indexes
> BarContainer;
但是如果我有这样一个类:
struct Foo {
Bar const& bar();
};
如何为 Foo
对象的容器在 .bar().property()
上构建索引?
通常我会嵌套调用 boost::bind
,但我不知道如何让它在多索引容器的上下文中工作。
最佳答案
与其提供用户定义的比较器,不如编写用户定义的键提取器:
struct FooBarPropertyExtractor { typedef std::string result_type; const result_type& oeprator()(const Foo& f) { return f.bar().property(); } }; ... typedef boost::multi_index_container< Bar, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::tag<tag_prop>, FooBarPropertyExtractor > > , ... other indexes > FooContainer;
关于c++ - 使用基于嵌套值的索引 boost 多索引容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1628321/