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

Content deleted Content added
m History: clean up; http->https (see this RfC) using AWB
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 171:
 
==Usage example==
<sourcesyntaxhighlight lang="cpp">
#include <iostream>
#include <string>
Line 197:
return 0;
}
</syntaxhighlight>
</source>
 
==Custom hash functions==
To use custom objects in std::unordered_map, a custom hash function must be defined. This function takes a const reference to the custom type and returns a size_t
<sourcesyntaxhighlight lang="cpp">
#include <unordered_map>
Line 211:
}
};
</syntaxhighlight>
</source>
 
The user defined function can be used as is in std::unordered_map, by passing it as a template parameter
<sourcesyntaxhighlight lang="cpp"> std::unordered_map<X,int,hash_X> my_map;</sourcesyntaxhighlight>
 
Or can be set as the default hash function by specializing the std::hash function
<sourcesyntaxhighlight lang="cpp">
namespace std {
template <>
Line 230:
//...
std::unordered_map<X,int> my_map;
</syntaxhighlight>
</source>
 
==References==