c++ - 使用基于嵌套值的索引 boost 多索引容器

标签 c++ boost nested multi-index

如果我有这样一个对象:

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;

参见 Advanced features of Boost.MultiIndex key extractors

关于c++ - 使用基于嵌套值的索引 boost 多索引容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1628321/

相关文章:

c++ - 使用空大括号初始化程序 : pointer or reference? 的重载分辨率

c++ - 无法获得与 POCO 一起使用的并发 HTTPS 请求

c++ - 使用 boost 信号编译 VS8 C++ 程序时出现问题

c++ - 错误 : ‘template’ (as a disambiguator) is only allowed within templates

c++ - 运行命名测试时使用 "disabled" boost 测试输出垃圾邮件

r - 如何组合rapply()和mapply(),或者如何递归使用mapply/Map?

c++ - 在 C++ 中,传递指针仍然复制对象?

C++11 迭代器和返回的 std::unique_ptr 的范围

python - 如何用破折号替换嵌套的短语列表?

sql - 在 SQL 中嵌套查询