Unordered associative containers (C++): Difference between revisions

Content deleted Content added
No edit summary
code --> source
Line 205:
==Custom hash functions==
In order to use custom objects in std::unordered_map, a custom hash functor must be defined. This function takes a const reference to the custom type and returns a size_t
<source lang="cpp">
<code>
struct X{int i,j,k;};
 
Line 213:
}
};
</codesource>
 
The user defined function can be used as is in std::unordered_map, by passing it as a template parameter
<codesource lang="cpp"> std::unordered_map<X,int,hash_X> my_map;</codesource>
 
Or can be set as the default hash function by specializing the std::hash function
<source lang="cpp">
<code>
namespace std {
template <>
Line 232:
//...
std::unordered_map<X,int> my_map;
</codesource>
 
 
==References==