C++ 对组合键使用Boost multi_索引

C++ 对组合键使用Boost multi_索引,c++,boost,c++14,C++,Boost,C++14,如何使用Boost multi_index将三个键组合成一个查询表达式?考虑下面的索引结构: struct indexItems { uint64_t x; // assume non-unique index std::string y; // assume unique within x std::string z; // assume unique within y }; 假设我想使用二进制AND运算符查询项目:x=1和y=“a”和z=“s”。我该怎么做 所有查询

如何使用Boost multi_index将三个键组合成一个查询表达式?考虑下面的索引结构:

struct indexItems {
    uint64_t x; // assume non-unique index
    std::string y; // assume unique within x
    std::string z; // assume unique within y
};
假设我想使用二进制AND运算符查询项目:x=1和y=“a”和z=“s”。我该怎么做

所有查询和插入都将使用x+y+z组合来插入、更新和删除multi_索引中的项目。除此之外,我还需要迭代按x排序的y和z


到目前为止,我发现的示例只涉及单个索引。

实际上,您不需要多索引容器。您可以这样做:

inline bool operator<(const indexItems& l, const indexItems& r) {
    return std::tie(l.x, l.y, l.z) < std::tie(r.x, r.y, r.z);
}

inline bool operator==(const indexItems& l, const indexItems& r) {
    return std::tie(l.x, l.y, l.z) == std::tie(r.x, r.y, r.z);
}

std::set<indexItems> items; // or use map with any second type
要迭代按x排序的所有值(第二次按y和z排序,虽然实际上并不需要,但也不会有任何影响):

作为奖励,如果您想查找
x
范围内的所有值:

auto it = items.lower_bound(indexItems{100});
auto end = items.upper_bound(indexItems{105});
for (; it != end; ++it)
    // ...

你所有的查询都处理x+y+z,还是你也在寻找任意组合,比如x+z和y+z查询?好问题。我将更新描述。当你说“我需要迭代按x排序的y和z”时,这是否意味着你需要获得具有特定y的所有值,然后按x的顺序迭代这些值?或者,这是否意味着要迭代所有按x排序的值?后者。我从不需要查询特定的y或z,而是对x和所有相关的y和z进行迭代。非常优雅。非常感谢。
for (const indexItems& item : items)
    // ...
auto it = items.lower_bound(indexItems{100});
auto end = items.upper_bound(indexItems{105});
for (; it != end; ++it)
    // ...