Boost 多索引容器库可以提供帮助。
更新:重读您的问题后,我认为您可能必须做 db 课程中所教的内容;您必须使用其中一个索引来创建临时结果,并根据您的第二个约束修剪该结果(或者,您可以在每个索引上运行每个约束,加入结果)。Boost Multi-index Containers AFAIK 一次只允许查找一个索引。
对于您询问的特定类型的查询,您可以通过在索引分区上添加索引来提供非常专业的数据结构来提供帮助。也就是说,例如,您取第二个索引,将其分成两半,为前半部分和第二部分重写第三个索引。然后你将每一半分开并重复。这样,您可以选择第二个索引的一部分,然后选择第三个索引的一部分(大约)在第二个索引的该部分内。但我不知道有任何图书馆这样做。
- 另一种方法可能是某种多维数据结构,尽管我不熟悉将这种结构与非数字键一起使用。例如,假设您的键是整数,您可以使用 3D kd-tree或其他空间索引,并查询三次范围。例如:(使用libssrckdtree,似乎它也可能适用于字符串):
(3) 的代码:
#include <ssrc/spatial/kd_tree.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <string>
typedef std::array<std::string, 3> Point;
typedef ssrc::spatial::kd_tree<Point, Point> Tree;
int main()
{
Tree tree;
Point point = {{"any1","aa1","cc1"}};
tree[point] = point;
point = {{"any2","ab1","cc1"}};
tree[point] = point;
point = {{"any3","ab1","cd1"}};
tree[point] = point;
point = {{"any4","ab1","cd2"}};
tree[point] = point;
point = {{"any5","ab1","cd3"}};
tree[point] = point;
point = {{"any22","ac1","cc1"}};
tree[point] = point;
point = {{"any33","ac1","cd1"}};
tree[point] = point;
point = {{"any44","ac1","cd2"}};
tree[point] = point;
point = {{"any55","ac1","cd3"}};
tree[point] = point;
point = {{"any6","aa1","cd2"}};
tree[point] = point;
point = {{"any7","aa1","cd3"}};
tree[point] = point;
Point lower{ { "any0", "ab", "cd" } }, upper{ { "any9", "ac", "cd2" } };
for(Tree::const_iterator it = tree.begin(lower, upper), end = tree.end();
it != end; ++it)
{
Point point = it->first;
Point value = it->second;
std::cout << point[0] << ", " << point[1] << ", " << point[2] << std::endl;
}
return 0;
}
输出:
any3, ab1, cd1
any4, ab1, cd2