c++ - boost 多索引 : lower_bound with value_type as argument

标签 c++ boost boost-multi-index

我想将 lower_bound 与 Boost MultiIndex 容器的 value_type 结合使用。到目前为止,我只能通过显式提取成员来完成这项工作:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/member.hpp>
#include <string>

struct name {
    std::string firstname;
    std::string lastname;
    name(const std::string & firstname, const std::string & lastname) :
        firstname(firstname), lastname(lastname) {}
};

typedef boost::multi_index::multi_index_container<
    name,
    boost::multi_index::indexed_by<
        boost::multi_index::ordered_unique<
            boost::multi_index::composite_key<
                name,
                boost::multi_index::member<name, std::string, &name::lastname>,
                boost::multi_index::member<name, std::string, &name::firstname>
            >,
            boost::multi_index::composite_key_compare<
                std::less<std::string>,
                std::less<std::string>
            >
        >
    >
> NameIndex;

int main(void) {
    NameIndex nameindex;
    nameindex.insert(name("Alfred", "Ammer"));
    nameindex.insert(name("Martin", "Mauser"));
    // In my real code, I get this object passed.
    name lookupname("Hans", "Hoffer");

    // Does not compile
    //auto it = nameindex.get<0>().lower_bound(lookupname);

    // compiles, but I have to take explicitly list the members - in the right order
    auto it = nameindex.get<0>().lower_bound(std::make_tuple(lookupname.lastname, lookupname.firstname));
}

如何避免提取成员?

最佳答案

auto it = nameindex.get<0>().lower_bound(
    nameindex.get<0>().key_extractor()(lookupname));

关于c++ - boost 多索引 : lower_bound with value_type as argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44886434/

相关文章:

c++ - operator= 的返回类型 - 引用还是值?

c++ - boost::mpl::bind 的使用

c++ - 在模板类中 boost 模板相关结构的多索引容器

c++ - boost 对散列唯一索引的多索引访问

c++ - 是否可以从代码中获取依赖库的位置

c++ - 数字对的有效搜索

c++ - 我如何使用位操作对单个整数中的两个数字进行编码和解码?

c++ - 将 boost::multiprecision 实数转换为整数

c++ - BOOST 版本 1.46.1 与 Visual Studio 2010 P.E

c++ - 获取 boost::multi_index 容器元素的等级