简体   繁体   English

在C ++ 0x标准中会有unordered_map,这与boost unordered_map相比如何?

[英]In the C++0x standard there will be unordered_map, how does this compare to boosts unordered_map?

Which is more efficient? 哪个更有效率? Are there any good benchmarks out? 有没有好的基准测试?

C++11's std::unordered_map specification is similar to boost::unordered_map which is based on tr1::unordered_map. C ++ 11的std :: unordered_map规范类似于boost :: unordered_map,它基于tr1 :: unordered_map。 That being said, there are some small differences. 话虽如此,但存在一些细微差别。 The addition of rvalue references in C++11 result in the addition of the emplace and emplace_hint functions that may be useful for performance. 在C ++ 11中添加rvalue引用会导致增加emplace和emplace_hint函数,这些函数可能对性能有用。

C++11 is now widely implemented and so you should be able to use std::unordered_map out of the box. C ++ 11现在已经广泛实现,因此您应该可以使用开箱即用的std :: unordered_map。 C++14 does not change it significantly and C++17 will (probably) add the insert_or_assign and try_emplace member functions. C ++ 14没有显着改变它,C ++ 17将(可能)添加insert_or_assign和try_emplace成员函数。

In the c++0x latest standard draft n3225, there's a section 23.6.1 class template unordered_map. 在c ++ 0x最新标准草案n3225中,有一节23.6.1类模板unordered_map。

So it is already there. 所以它已经存在了。

C++0x unordered_map is proposed based on boost one. 基于boost one提出了C ++ 0x unordered_map。 Boost library itself also has a namespace tr1::unordered_map, which shares the implementation of its own boost::unordered_map. Boost库本身也有一个命名空间tr1 :: unordered_map,它共享自己的boost :: unordered_map的实现。

If you want to compare (of course you don't need to compare boost with boost), I think several other compilers, including microsoft visual studio 2010, and gcc, do have their own unordered_map implementation. 如果你想比较(当然你不需要比较boost和boost),我认为其他几个编译器,包括microsoft visual studio 2010和gcc,都有自己的unordered_map实现。 You can use them by assuming they are under namespace tr1. 您可以通过假设它们位于命名空间tr1下来使用它们。

#include <unordered_map>
...
std::tr1::unordered_map<...>

I didn't know any benchmark yet but I think at this early time, any benchmarking doesn't make sense because the compiler implementer will definitely optimize their own implementations when the real standard is finalized and more people are going to use the library. 我还不知道任何基准测试,但我认为在这个早期,任何基准测试都没有意义,因为当真正的标准最终确定并且更多人将使用该库时,编译器实现者肯定会优化他们自己的实现。

One minor point not yet mentioned, the std::hash function is only required to be able to compute hashes of builtin types and strings (and a few other types). 还没有提到的一个小问题是, std::hash函数只需要能够计算内置类型和字符串(以及其他一些类型)的哈希值。 The boost::hash function can compute hashes of more complex objects such as pair and tuple . boost::hash函数可以计算更复杂对象的散列,例如pairtuple Also boost has a hash_combine function to assist in creating hashes for user-defined types. boost还有一个hash_combine函数,可以帮助为用户定义的类型创建哈希值。

This means that std::unordered_set< pair<int, int> > won't compile, but boost::unordered_set< pair<int, int> > will. 这意味着std::unordered_set< pair<int, int> >不会编译,但boost::unordered_set< pair<int, int> >将会。

You can use boost::hash with std::unordered_* if needed. 如果需要,您可以将boost::hashstd::unordered_*使用。

(Reference: Item 6.18 in the Library Extension Technical Report Issues List .) (参考: 图书馆扩展技术报告问题清单第6.18项。)

It depends on the implementation and the data set in question. 这取决于实施和相关数据集。 When I was playing around with unordered_map for a blog post I found that VS10's std::unordered_map perfromed much worse than boost::unordered_map for the input I used (I didn't build a thorough benchmark). 当我在博客文章中使用unordered_map ,我发现VS10的std::unordered_map boost::unordered_map 对于我使用的输入 (比如我没有建立彻底的基准测试),比boost::unordered_map更糟糕。 In theory thought there shouldn't be a difference. 理论上认为应该没有区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM