双向关联容器

two way associative container

我正在寻找一种数据类型(或至少是它的正确名称)或类似于地图的数据结构,可以在两个方向上进行快速查找。

类似于:

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)