【问题标题】:two way associative container双向关联容器
【发布时间】:2016-02-01 10:45:45
【问题描述】:

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

类似:

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 的东西?

【问题讨论】:

  • bimap 可能是你所追求的
  • @EdChum 谢谢。可能会将此作为答案发布。

标签: c++ qt boost c++-standard-library


【解决方案1】:

我建议使用boost::bimap,这是专为按键或值查找而设计的。

所以在你的情况下,你可以这样做:

#include <boost/bimap.hpp>
typedef boost::bimap< int, int > bm_type;
bm_type doubleMap;

然后按键查找:

doubleMap.left.find(key)

按值查找:

doubleMap.right.find(val)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 2013-08-27
    • 2018-08-24
    • 2013-12-02
    相关资源
    最近更新 更多