2016-02-01 94 views
1

我正在尋找一種數據類型(或至少正確的名稱)或數據結構之類的地圖,以便在兩個方向上快速查找。雙向關聯容器

類似:

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; 

}; 
當然模板和更先進的

是否有一個名稱爲它和一些std容器或從Qt或提升?

+3

['bimap']( http://www.boost.org/doc/libs/1_60_0/libs/bimap/doc/html/index.html)可能就是你要的 – EdChum

+0

@EdChum謝謝你。可能會將此作爲答案。 –

回答

3

我建議使用boost::bimap這是設計用於按鍵或值查找。

所以你的情況,你可以這樣做:

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

然後通過按鍵來進行查找:

doubleMap.left.find(key) 

查找按值:

doubleMap.right.find(val)