Tim*_*ter 6 c++ boost nested multi-index
如果我有这样的对象:
struct Bar {
std::string const& property();
};
Run Code Online (Sandbox Code Playgroud)
我可以像这样为它创建一个多索引容器:
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;
Run Code Online (Sandbox Code Playgroud)
但如果我有这样一个类:
struct Foo {
Bar const& bar();
};
Run Code Online (Sandbox Code Playgroud)
如何.bar().property()为Foo对象容器构建索引?
通常我会嵌入调用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;
我相信你需要创建一个带有两个Foo实例的谓词对象,它的operator()可以在两个实例上调用Foo :: bar().
就像是
struct MyPredicate
{
bool operator() (const Foo& obj1, const Foo& obj2) const
{
// fill in here
}
};
Run Code Online (Sandbox Code Playgroud)
然后使用
...
boost::multi_index::ordered_unique<boost::multi_index::tag<tag_prop>,
boost::multi_index::identity<Foo>, MyPredicate>,
...
Run Code Online (Sandbox Code Playgroud)
查看MultiIndex Ordered indices参考
| 归档时间: |
|
| 查看次数: |
1982 次 |
| 最近记录: |