我正在寻找一种数据类型(或至少是它的正确名称)或类似于 map 的数据结构,以允许在两个方向上进行快速查找。
类似于:
class DoubleMap{
int getA(int b){
return b2a[b];
}
int getB(int a){
return a2b[a];
}
void insert(int a, int b){
a2b[a] = b;
b2a[b] = a;
}
std::map<int, int> a2b;
std::map<int, int> b2a;
};
当然是模板化和更复杂的。
是否有它的名称和一些标准容器或来自 Qt 或 boost 的东西?
最佳答案
我建议使用 boost::bimap
这是为按键或值查找而设计的。
所以在你的情况下你可以这样做:
#include <boost/bimap.hpp>
typedef boost::bimap< int, int > bm_type;
bm_type doubleMap;
然后按键执行查找:
doubleMap.left.find(key)
按值查找:
doubleMap.right.find(val)
关于c++ - 双向关联容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35128535/