地图可以在大​​约相同的成本都值和键搜索? Symethric地图吗? [英] Map that can search in both values and keys about the same cost? Symethric map?

查看:128
本文介绍了地图可以在大​​约相同的成本都值和键搜索? Symethric地图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在一个&LT达50项;无符号长,为const char *> 地图。这样做的原因是这样的协议,我使用引用由inital握手后的数字串的名字 - 我认为像我必须在服务器上存在类似的地图

I am about to have up to 50 entries in a <unsigned long, const char*> map. The reason for this is that protocol I'm using references string names by numbers after inital handshake - I assume similar map as mine must exist on the server.

我想要的是可搜索两个键和值(两者都保证是独一无二的,结合在一起)大约在同一code的复杂性和性能代价的​​地图。一个想法是:

What I wanted was a map that can be searched for both key and value (both are guaranteed to be unique and bound together) with about the same code complexity and performance cost. One idea was:

symethric_map<unsigned long, const char*> map;
map[165]="NODE_A";
std::cout<<map->inverse["NODE_A"]<<std::endl; // Should print 165

在此code的想法是, symethric_map&LT; T,N&GT; ::逆是一个对象,可以把映射键作为值,反之亦然。当然,地图用简单的 searchValue 的方法将解决这个问题了。

The idea in this code is that symethric_map<T,N>::inverse is an object that can treat map keys as values and vice versa. Of course, map with simple searchValue method would solve that problem too.

我问这个,因为我会不断地解决数字为字符串,反之亦然,没有任何可靠的数字生成系统,要做到这一点的唯一方法是记住所有的关系。 的std ::矢量可用于如数字分别为序,这将不会八方通如此。我愿意用任何增强功能以​​及C ++ 11的把戏。

I am asking for this because I will be constantly resolving number to strings and vice versa and without any reliable number generating system, the only way to do that is to remember all the relations. std::vector could be used if the numbers were ordinal, which will not allways be the case. I am willing to use any boost feature as well as C++11 tricks.

推荐答案

Simples用例:

Simples use-case:

<大骨节病> 住在Coliru

#include <boost/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <iostream>

namespace bm = boost::bimaps;

using symethric_map = bm::bimap<bm::set_of<unsigned long>, bm::set_of<const char*, std::less<std::string> > >;

int main() {
    symethric_map map;
    map.left.insert({165, "NODE_A"});
    std::cout << map.right.at("NODE_A") << "\n";
}

打印

165

这篇关于地图可以在大​​约相同的成本都值和键搜索? Symethric地图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆