C++ Standard Library C++ STL Library

C++ unordered_set - hash_function() Function



The C++ unordered_set::hash_function function returns the hash function object used by the unordered_set 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_set::hash_function().

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

  unordered_set<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_set> Library