Internship at OpenGenus

Get this volume -> Bug on Array: For Interviews and Competitive Programming

Reading time: 25 minutes | Coding time: v minutes

In this commodity, nosotros will cover how hashing is natively supported in C++ through STL (std:hash) and how we tin use it to generate hash of any object in C++.

When we utilise any associative container we unknowingly use an hash function. Basically the fundamental-value pairs that we use in a map, multimap, unordered_map etc are mapped with each other. The footing of mapping comes from the hashcode generation and the hash function.

std::hash is a class in C++ Standard Template Library (STL). Information technology is such a class that can exist constructed in a more dafault way which in others words means that any user who intends to use the hash grade tin can constuct the objects without whatsoever given initial values and arguments. So by default a hash class is a template class.

We will cover the following sub-topics:

  • How to create an object in std::hash?
  • Character hashing
  • Cord hashing
  • Integer hashing
  • Vector hashing
  • Hasing and collision

How to create an object in std::hash?

To create a objeect for the hash course nosotros take to follow the following process:

              hash<course template> object_name;                          

Member functions

The std::hash form only contains a unmarried fellow member function.

operator(): this returns the hashed value for each object argument that is provided to it.

Now lets directly switch over to different objects that tin can be used in the hash function to get their corresponding hash values.

Character hashing

              #include<bits/stdc++> using namespace std; void character_Hashing()  {     char ch = 'b';     hash<char> hash_character;      cout << "\nthe hashed value is: " << hash_character(ch) << endl;  }  int principal()  {      character_Hashing();  }                          

Output:

              the hashed value is: 98                          

String hashing

To hash a string in C++, use the following snippet:

              hash<char> hash_string; hash_string(s); // s is a cord                          

This C++ lawmaking instance demonstrate how string hashing can exist achieved in C++.

              #include<$.25/stdc++> using namespace std; void string_Hashing()  {     string s = "abc";     hash<char> hash_string;      cout << "\nthe hashed value is: " << hash_string(southward) << endl;  }  int main()  {      string_Hashing();  }                          

Output:

              the hashed value is:  3665446528845549387                          

At present for an integer the hash function returns the same value as the number that is given as input.The hash function returns an integer, and the input is an integer, so but returning the input value results in the most unique hash possible for the hash type.

Integer hashing

To hash an integer in C++, use the following snippet:

              hash<int> hash_string; hash_string(n); // n is an integer                          

This C++ lawmaking example demonstrate how integer hashing tin can be achieved in C++.

              #include<$.25/stdc++> using namespace std; void int_Hashing()  {     int due north = 5;     hash<int> hash_int;      cout << "\nthe hashed value is: " << hash_int(n) << endl;  }  int primary()  {      int_Hashing();  }                          

Output:

              the hashed value is: 5                          

Vector hashing

To hash a vector in C++, use the following snippet:

              // define the vector vector<bool>          bol{ truthful, faux,                     truthful, false };     // create the hash part hash<vector<bool> h_f> ;  // use the hash function h_f(bol);                          

This C++ code instance demonstrate how vector hashing tin exist accomplished in C++.

              #include<bits/stdc++.h> using namespace std; void vector_Hashing()  {       vector<bool>          bol{ truthful, false,                     true, false };         hash<vector<bool> h_f> ;      cout << "\northward hash value: "<< h_f(bol) << endl;  }   void principal() {     vector_Hashing(); }                          

output

              hash value: 16935082123893034051                          

Apart from these standard data types, nosotros can as well use hash function for many other information types:

  1. boolean
  2. signed char
  3. unsigned char
  4. brusque
  5. unsigned sort
  6. signed sort
  7. bladder
  8. double
  9. long double
  10. long

and others.

So these were some types of information that tin can be hashed using the std::hash class.
the real use of std::hash comes in a hashtable where different fundamental values are to exist hashed to thier values.

All these hashing stuff that the hash functions do is just to find a unique identity all the arguments that demand to exist hashed and farther referred using their hashed values.

Hasing and collision

Now the demand of a good hash function is surely a concern because collision can exist bad issue in case of a hash function that is not that diverse and robust. And then sometimes it is also required we build some user defined hash part so that in standoff cases nosotros can handle it properly.

That was almost everything nigh std::hash. Hope yous liked it.

Happy coding :)