提高:: bimap的,枚举 [英] boost::bimap for enum

查看:190
本文介绍了提高:: bimap的,枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建在C ++枚举一个简单的双向查找工具。我有一个单向的查找工作正常...

I'm trying to create a simple bi-directional lookup facility for enums in C++. I have one-way lookup working fine...

enum MyEnum
{ 
    One, 
    Two, 
    Three 
};

const boost::unordered_map<MyEnum,std::string> MyEnumMap = map_list_of
    (One, "One")
    (Two, "Two")
    (Three, "Three");

,然后做通过

MyEnumMap.at(One)

这工作,但只允许基于键查找。我想用一个双向查找容器如boost:bi​​map的允许基于价值以及重点简单的反向查找。它似乎并不像map_list_of是的boost :: bimap的,虽然兼容。

That works but it only allows for lookups based on key. I'd like to use a bi-directional lookup container such as boost:bimap to allow for easy reverse lookups based on value as well as key. It doesn't seem like map_list_of is compatible with boost::bimap though.

首先,我应该仍然在使用map_list_of用的boost :: bimap的,或者是否需要另一种类型?结果
这些地图都将是一个基本的(枚举,字符串)类型。

Firstly, should I still be using map_list_of with boost::bimap, or is another type required?
The maps will all be a basic (Enum, string) type.

第二,是有办法,我仍然可以定义地图为const按上述的简单方法?我试图使这个容易更新和维护尽可能没有进入过多额外的typedef和这样的。你的洞察力AP preciated。

Second, is there a way I can still define the map as const in a simple way as above? I'm trying to make this as easy to update and maintain as possible without getting into too many additional typedefs and such. Your insight is appreciated.

推荐答案

尝试使用直接LIST_OF:

Try using list_of directly:

typedef boost::bimap< MyEnum, std::string > bm_type;
const bm_type MyEnumMap =
  boost::assign::list_of< bm_type::relation >
    ( One, "One" )
    ( Two, "Two" )
    ( Three, "Three" );

请注意,而不是'VALUE_TYPE'关系'的用法。

Note the usage of 'relation' instead of 'value_type'.

这篇关于提高:: bimap的,枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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