在c ++ 11中有没有Boost.Bimap替代? [英] Is there a Boost.Bimap alternative in c++11?

查看:203
本文介绍了在c ++ 11中有没有Boost.Bimap替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想避免使用Boost,但完全拥抱C ++ 11。如果需要,在我的程序中,Boost的bimap的一个细微的版本将适用于我(我需要一个常量的bimap来在枚举和相应的字符串之间切换)。地图将编译时不变,所以也许甚至两个手动维护的地图不是最佳的解决方案。



谢谢!



更新:我在代码项目中发现这一点,但似乎许可可能是一个问题: http://www.codeproject.com/KB/stl/bimap.aspx ?fid = 12042& df = 90& mpp = 25& noise = 3& sort = Position& view = Quick& fr = 151#xx0xx



'只是寻找一个干净简单的解决方案(一个标题/源文件或一点额外,因为两个镜像地图在我的情况下同样好)。

解决方案

我的感觉很多,Boost图书馆的工作正在使他们与其他图书馆/ STL一起工作。



如果你不需要这个能力,你可以使用一个类,一个 std :: map< X *,Y *> std :: map< Y *,X *> 。然后使用以下方法: add(X,Y) remove(X,Y) get_left(X) get_right(Y)



要存储副本, add(X,Y)可以分配内存,而 remove(X,Y) can de -分配。此外,您可以定义一个析构函数,它在其余元素上调用 remove(X,Y)


Is there a usable alternative to Boost's bimap in C++0x?

I would like to avoid Boost, but fully embrace C++11. If necessary, a slimmed down version of Boost's bimap would work for me (I need a constant bimap to switch between enums and corresponding strings) throughout my program. The map will be compile-time constant, so perhaps even two manually maintained maps aren't the optimal solution.

Thanks!

UPDATE: I found this on The Code Project, but it seems licensing may be an issue: http://www.codeproject.com/KB/stl/bimap.aspx?fid=12042&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=151#xx0xx

I'm just looking for a clean and easy solution (one header/source file or little extra, as two mirrorred maps are equally fine in my case).

解决方案

My feeling is a lot of the work that goes into Boost libraries is making them work with other libraries/STL.

If you don't need that capability, you could just use a class with a std::map<X*, Y*> and std::map<Y*, X*>. Then have methods like the following: add(X,Y), remove(X,Y), get_left(X) and get_right(Y).

If you want to store copies, add(X,Y) could allocate memory, and remove(X,Y) can de-allocate. Also, you can then define a destructor that calls remove(X,Y) on the remainder of the elements.

这篇关于在c ++ 11中有没有Boost.Bimap替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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