Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library

The C++ unordered_set::hash_function function is used to return 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.


hasher hash_function() const;


No parameter is required.

Return Value

The hash function.

Time Complexity

Constant i.e, Θ(1)


The below example 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