Question

Looking around on cppreference, I found that std::unordered_map gets efficient lookup functions from "equivalent keys".

I take that to mean that the equivalent key must have the same hash value. How can I provide that for a string literal I get the same hash value as for std::hash<std::string> without temporarily constructing an std::string and thereby making the whole point about the equivalent keys for naught?

Was it helpful?

Solution

That was an error in cppreference; there are no templated finds for unordered associated containers.

Compare, from n3690,

from §23.5.4.1[unord.map.overview]

// lookup
iterator find(const key_type& k);
const_iterator find(const key_type& k) const;
size_type count(const key_type& k) const;

from §23.4.4.1[map.overview]

// 23.4.4.5, map operations:
iterator find(const key_type& x);
const_iterator find(const key_type& x) const;
template <class K> iterator find(const K& x);
template <class K> const_iterator find(const K& x) const;
size_type count(const key_type& x) const;

OTHER TIPS

Heterogeneous lookup for unordered containers has been adopted for C++20 (p0919, p1690). It's already available in gcc 11.0 and clang 12.0.

As others said, unordered associative containers don't support the is_transparent mode. Boost.MultiIndex hashed indices, on the other hand, allow for what you want, as explained in the documentation, in case it is an option for you to replace std::unordered_map with an equivalent construct based on a multi_index_container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top