c++11 中有 Boost.Bimap 替代品吗?
C++0x 中是否有 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
我只是在寻找一个干净且简单的解决方案(一个头文件/源文件或额外的东西,因为在我的情况下两个镜像映射同样好)。
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.
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答:不。
长答案:不。
应该注意的是,C++14 对透明比较器的支持消除了这种需要对于 Boost.Bimap 90% 的时间*:当您需要关闭对象的任何给定属性(存储或计算),通常是对象固有的/存在于对象中的简单的、可按位比较的唯一标识符。使用透明比较器,您可以将对象与任何可能的值进行比较,仅按类型进行区分,只要可以从对象获取/计算所述值而不改变它即可。
*猜测,而不是统计数据
Short answer: no.
Long answer: nope.
It should be noted that C++14's support for transparent comparators eliminates the need for Boost.Bimap 90% of the time*: when you need to key off of any given property of an object (stored or computed), often a simple, bitwise-comparable unique identifier inherent to/present in the object anyway. With transparent comparators, you can compare an object to any possible value, discriminated only by type, as long as said value can be obtained/computed from an object without mutating it.
* a guesstimate, not a statistic
我的感觉是,Boost 库中的大量工作使它们能够与其他库/STL 一起工作。
如果您不需要该功能,则可以仅使用带有
std::map
和std::map
。然后使用如下方法:add(X,Y)
、remove(X,Y)
、get_left(X)
和get_right(Y)
。如果要存储副本,
add(X,Y)
可以分配内存,remove(X,Y)
可以取消分配。此外,您还可以定义一个析构函数,对其余元素调用remove(X,Y)
。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*>
andstd::map<Y*, X*>
. Then have methods like the following:add(X,Y)
,remove(X,Y)
,get_left(X)
andget_right(Y)
.If you want to store copies,
add(X,Y)
could allocate memory, andremove(X,Y)
can de-allocate. Also, you can then define a destructor that callsremove(X,Y)
on the remainder of the elements.