C++ hash introduction

In C++, a hash is a function used to create a hash table. When this function is called, it generates an address for each key, which is given in the hash function. If a hash function returns a unique hash number, the hash function is called a generic hash function. The C++ standard library provides a class called hash, which can be constructed without passing any arguments, so in general, the hash function is used for hashing, which maps keys to values to form the data structure of a hash table, and this function computes the index of an array.

Hash function in C++ working principle and examples

In this article, we’ll look at a hash class defined in the C++ standard library as STD ::hash, which allows the user to create a hash class that can construct objects without initializing values and parameters, so to say that a hash class is a template class. Therefore, the main purpose of using a hash is to make the search faster. This is done with an index of each value located in memory. A hash function has a key (index) that points to the address of the value located in memory and can be retrieved faster by using its key. In C++, a hash function is a function whose key points to a value, which is an address; When this function is called, it uses a combination of letters and numbers in the hash table, which can be used to arrange data.

Collisions between two or more keys that point to the same value are possible and can be avoided by using chained hashes, which point to linked list records because each cell in the hash table has the same value and the hash key maps to the hash function value. So, in real time, we can associate this hash function or hash table with the phone book, where each name is a key and the phone number is the value of the key, which is the name.

A hash class can be defined using the STL library in C++, which is used to get the hash value of the argument passed to it. Let’s look at the syntax below.

Syntax.

template <class key> struct hash;

Above, we can see that we have a syntax for writing or creating a hash class, and then we can use the following syntax to create an object in the STD :: Hash class.

Syntax.

hash<class template> obj_name ;

So, in order to add some items to the hash table, we need to have a hash function that uses the hash index of the given key, which must be computed using a hash function such as “hash_inx = key % num_of_slots(size of the hash table) “, for example, Hash table size is 10, key value (item) is 48, then hash function =43%10 = 3; Therefore, the hash code will be 3, which means that 43 items are placed in index 3 of the hash table. The project may sometimes occur in the same index of the collision, suppose that we had 63 keys, so also will produce three hash code, with the keys and collision, so in order to avoid this type of collision or solve this type of problem, we can use the open hash or single chain, the implementation way and the list of links, Another solution is to use linear probing, allowing all entries to be stored in the hash table itself, and many other ways to solve this collision problem.

As a result, the hash table is an array has a certain size, it has a hash function, from the object map to the hash table within the project, these objects are placed in the hash table, it’s like an array, each object has an index, through the hash function to calculate the index = h (objects), so the array is called a hash table. This hash class has only one member function, operator(), which returns the hash value of the argument passed to the member function. So, below, let’s use a simple program to get the hash values of the corresponding hash functions that use various objects.

Example.

#include <iostream> #include <string> using namespace std; void strhashing() { string h1 = "Educba"; cout <<"The string given to get the hash value is "<< h1 <<"\n"<<endl; hash<string> hash_obj; cout << "The hash value of the given string is : " << hash_obj(h1)<< endl; } int main() { cout<<"Program to demonstrate the string hash values that are returned using hash class and its objects."<<"\n"<<endl; strhashing(); }

The output.

In the above program, we can see that we are defining the function strhashing(), we are declaring a string “h1”, we are trying to get the hash value of the given string “Educba “, first we will create a hash object “hash_obj”, We pass the given string as an argument to the created hash object, which will display the string hash, and the hash value of the given string “Educba “is 11677389314383596536, as shown in the screenshot above. Therefore, there are many other data types besides string data types, and a hash function can be used to hash the value of each data type, such as char, vector, Boolean, float, double, long, and so on.

So now let’s try to create a hash table in the C++ programming language, using hash function values in the following example.

Example.

#include <iostream> #include <list> using namespace std; class hash_table{

Private.

list<int> *tbl; int all_ele; int fetch_hash(int k){ return k % all_ele; }

public:

hash_table(int a){ all_ele = a; tbl = new list<int>[all_ele]; } void inst_ele(int k){ tbl[fetch_hash(k)].push_back(k); } void disp(){ for(int i = 0; i < all_ele; i++){ cout << "The Index of item is " << i << "\n " <<endl; for(int j : tbl[i]) cout <<"The value for the index "<<i << " is " << j << endl; cout << endl; }}}; int main() { hash_table kh(3); int a[] = {2, 4, 6}; for(int i = 0; i < 3; i++) kh.inst_ele(a[i]); cout << "The hash table is created is as follows: " << "\n"<< endl; kh.disp(); return 0; }

The output.

In the above program, we can see that we are declaring an array and trying to insert each item into the hash table. We first evaluate the hash function, which gives us the index value that can be used to place the item. So in the screenshot above, we can see that we have placed the item in the specific index shown in the output.

conclusion

In this article, we conclude that hash in C++ is a function for creating hash tables that are useful for searching any item quickly and easily. In this article, we saw the syntax for using the syntax of a hash class to create an object of a hash class. In this article, we also looked at an example of getting a hash value of a datatype variable. In this article, we also saw how to create a hash table and how to insert elements into the hash table.

Recommend the article

This is a guide to C++ hashing. Here we discuss how hash functions work in C++ and examples of their output. You can also check out the article below to learn more

  1. C++ Max
  2. coding
  3. Length of array
  4. C++ thread( )

The postC++ hashappeared first onEDUCBA.