C++ Standard Library C++ STL Library

C++ unordered_map - hash_function() Function



The C++ unordered_map::hash_function function returns the hash function object used by the unordered_map container. The hash function is unary function which takes key_type object as argument and returns unique value of type size_t based on it.

Syntax

hasher hash_function() const;

Parameters

No parameter is required.

Return Value

The hash function.

Time Complexity

Constant i.e, Θ(1)

Example:

The example below shows the usage of unordered_map::hash_function().

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_map<string, string> uSet;

  unordered_map<string, string>::hasher hfun = uSet.hash_function();

  cout<<"The hash function for alpha: "<<hfun("alpha")<<"\n";
  cout<<"The hash function for Alpha: "<<hfun("Alpha")<<"\n";

  return 0;
}

The output of the above code will be:

The hash function for alpha: 6998202458872244694
The hash function for Alpha: 6500479057991640117

❮ C++ <unordered_map> Library