Skip to content
Related Articles

Related Articles

unordered_set hash_function() in C++ STL
  • Difficulty Level : Medium
  • Last Updated : 28 Sep, 2018

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

Syntax:

unordered_set_name.hash_function()

Parameter: The function accepts no parameter.

Return Value: The function returns the hash function.

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



Program 1:




// CPP program to illustrate the
// unordered_set::hash() function
  
#include <iostream>
#include <string>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<string> sampleSet = { "geeks1", "for", "geeks2" };
  
    // use of hash_function
    unordered_set<string>::hasher fn = sampleSet.hash_function();
  
    cout << fn("geeks") << endl;
  
    for (auto it = sampleSet.begin(); it != sampleSet.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
  
    return 0;
}


Output:

15276750567035005396
geeks2 geeks1 for

Program 2:




// CPP program to illustrate the
// unordered_set::hash() function
  
#include <iostream>
#include <string>
#include <unordered_set>
  
using namespace std;
  
int main()
{
  
    unordered_set<string> sampleSet;
  
    // use of hash_function
    unordered_set<string>::hasher fn = sampleSet.hash_function();
  
    cout << fn("geeksforgeeks") << endl;
  
    return 0;
}


Output:

5146686323530302118

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :