Which is more efficient? Are there any good benchmarks out?
The difference between an unordered_map and an unordered_set is that an unordered_map stores data only in the form of key-value pair while an unordered_set can store data that is not necessarily in the form of key-value pairs (example integer, string, etc.).
Use std::unordered_map when You need to keep count of some data (Example – strings) and no ordering is required. You need single element access i.e. no traversal.
Insertion of spread keys in std::map tends to outperform std::unordered_map when map size is under 10000 elements. Insertion of dense keys in std::map doesn't present performance difference with std::unordered_map under 1000 elements. In all other situations std::unordered_map tends to perform faster.
As you can see, using the unordered_map is substantially faster than the map implementation, even for small numbers of elements.
C++11's std::unordered_map specification is similar to boost::unordered_map which is based on 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 is now widely implemented and so you should be able to use std::unordered_map out of the box. C++14 does not change it significantly and C++17 will (probably) add the insert_or_assign and try_emplace member functions.
In the c++0x latest standard draft n3225, there's a section 23.6.1 class template unordered_map.
So it is already there.
C++0x unordered_map is proposed based on boost one. Boost library itself also has a namespace tr1::unordered_map, which shares the implementation of its own 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. You can use them by assuming they are under namespace 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With