按键和值搜索对称关联数组

时间:2010-07-17 23:32:42

标签: c++ arrays associative

我需要描述一个可在其中搜索的关联数组,您可以使用键和值。使用函数add,delete,getBy1st(按键搜索),getBy2nd(按值搜索)。 例如在C ++中:

symmap<std::string, int> m;  
m.insert(make_pair<std::string,int> ("hello", 1));
m.insert(make_pair<std::string,int> ("wow", 2));
...
m.getBy1st("hello"); // returns 1
m.getBy2nd(2);// returns "wow"

它应该适用于O(log(n))并存储在std :: pair中。 我无法确定用于存储的数据结构。 也许我可以使用rb-tree的一些变体来存储它?

2 个答案:

答案 0 :(得分:2)

这听起来很像Boost.Bimap

答案 1 :(得分:1)

为什么不使用一对哈希表来存储数据 - 一个从T1到T2进行散列,另一个散列在另一个方向?

相关问题