Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

unordered_multimap hash_function() in C++ STL

  • Last Updated : 08 Aug, 2018

The unordered_multimap::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:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
unordered_multimap_name.hash_function()

Parameter: The function does not accept any parameter.

Return Value: The function returns the hash function.



Below programs illustrate the unordered_multimap::hash_function() function:

Program 1:




// C++ program to illustrate the
// unordered_multimap::hash_function()
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<string, string> sample;
  
    // inserts key and elements
    sample.insert({ "gopal", "dave" });
    sample.insert({ "gopal", "dave" });
    sample.insert({ "gopal", "dave" });
    sample.insert({ "geeks", "geeks" });
  
    // use of hash_function
    unordered_multimap<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 << "{" << it->first << ":"
             << it->second << "}, ";
    }
    return 0;
}
Output:
15276750567035005396
Key and Elements: {geeks:geeks}, {gopal:dave}, {gopal:dave}, {gopal:dave},

Program 2:




// C++ program to illustrate the
// unordered_multimap::hash_function()
#include <iostream>
#include <unordered_map>
using namespace std;
  
int main()
{
  
    // declaration
    unordered_multimap<string, string> sample;
  
    // insertion of key and elements
    sample.insert({ "gopal", "dave" });
    sample.insert({ "geeks", "geeks" });
  
    // use of hash_function
    unordered_multimap<string, string>::hasher fn
                                      = sample.hash_function();
  
    cout << fn("geeksforgeeks") << endl;
  
    // print the key and elements
    cout << "Key and Elements: ";
    for (auto it = sample.begin(); it != sample.end(); it++) {
        cout << "{" << it->first << ":"
             << it->second << "}, ";
    }
    return 0;
}
Output:
5146686323530302118
Key and Elements: {geeks:geeks}, {gopal:dave},



My Personal Notes arrow_drop_up
Recommended Articles
Page :