Boost::Multiindex 与字符串索引 boost::unordered_map
我需要一个包含唯一元素的容器,可以使用 int 三元组来访问,每个 int 可以超过 1.000.000.000。
(这些元素中只有少数会被实际填充,实际上这些元素本身就是 boost::unordered_map )。
使用像 boost::multiindex (或者其他我不知道的东西)这样的多索引数组或者只是一个以组合字符串作为键的 boost::unordered_map 更快吗?
I need a container of unique elements to be accessed with a triplet of int, and each int can be over 1.000.000.000.
(Only few of these elements will be actually filled, and actually these elements are boost::unordered_map themselves).
Is it faster to have a multiindex array like boost::multiindex (or maybe something else I don't know) or just a boost::unordered_map with a composed string as a key ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多索引不是您想要的,您似乎想要一个类型为三元组的单个索引。 (除非您确实想要三个独立的索引;如果我误解了,请发表评论。)
不要使用字符串,天哪。只需使用三元组作为键:
如果您使用
std::map
,您将获得对数查找,这可能就足够了,我认为您甚至不必这样做任何更多的工作(不确定是否默认为元组定义了字典比较)。如果您想使用
std::unordered_map
(或 boost 版本),则必须定义一个哈希函数。我认为 Boost 已经有一个元组了,但是 C++11 没有;但基于hash_combine()
自己实现非常容易,您只需剪掉 Boost 代码即可。Multi-index isn't what you want, you seem to want a single index whose type is a triple. (Unless you actually do want three independent indexes; if I misunderstood, leave a comment.)
Don't use strings, heavens no. Just use the triple as a key:
If you use an
std::map<key_type, T>
, you get logarithmic lookup, which may be sufficient, and I think you don't even have to do any more work (not sure if lexicographic comparison is defined by default for tuples).If you want to use an
std::unordered_map<key_type, T>
(or the boost version), you have to define a hash function. Boost already has one for tuples, I think, but C++11 doesn't; but it's very easy to implement yourself based onhash_combine()
which you can just crop out off the Boost code.