unordered_map hash_function() function in C++ STL

The unordered_map::hash_function() is a built in function in C++ STL which is used to get the hash function. This hash function is a unary function which takes a single argument only and returns a unique value of type size_t based on it.

Syntax:

unordered_map_name.hash_function()

Parameter: The function does not accept any parameter.

Return Value: The function returns the hash function.

Time complexity: Time Complexity of this function is constant O(1).



Below programs illustrate the unordered_map::hash_function() function.

Example 1

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_map::hash_function()
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_map<string, string> sample;
  
    // inserts key and elements
    sample.insert({ "Ankit", "MNNIT" });
    sample.insert({ "Ram", "MNNIT" });
    sample.insert({ "Manoj", "Trichy" });
    sample.insert({ "geeks", "geeks" });
  
    // use of hash_function
    unordered_map<string, string>::hasher fn
        = sample.hash_function();
  
    cout << fn("geeks") << endl;
  
    // print the key and elements
  
    cout << "Key and Elements: ";
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "\n{" << it->first << ":"
             << it->second << "}, ";
    }
    return 0;
}

chevron_right


Output:

15276750567035005396
Key and Elements: 
{geeks:geeks}, 
{Manoj:Trichy}, 
{Ankit:MNNIT}, 
{Ram:MNNIT},

Example 2

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// unordered_map::hash_function()
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_map<int, int> sample;
  
    // inserts key and elements
    sample.insert({ 1, 5 });
    sample.insert({ 2, 6 });
    sample.insert({ 3, 6 });
    sample.insert({ 4, 7 });
  
    // use of hash_function
    unordered_map<int, int>::hasher fn
        = sample.hash_function();
  
    cout << fn(4) << endl;
  
    // print the key and elements
  
    cout << "Key and Elements: ";
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "\n{" << it->first << ":"
             << it->second << "}, ";
    }
    return 0;
}

chevron_right


Output:

4
Key and Elements: 
{4:7}, 
{3:6}, 
{1:5}, 
{2:6},

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.