The use of hash table in C++

The STL in C++ provides hash_map to implement the hash table function. Before introducing the use of hash_map, let's understand the hash table from the hash function and hash conflict.

1. Hash function

The so-called hash function is the mapping from the key (Key) to the value (Value): Value
= H (K ey) Value=H(Key)Value=The value of H ( K e y )
reflects the storage address of the keyword.
1. Numerical analysis method
Select a few digits in the keyword as the value, and generally select a few digits with a relatively even distribution of numbers.
H (k 1 k 2 k 3 k 4 k 5 k 6 k 7) = k 2 k 3 k 5 H(k_1 k_2 k_3 k_4 k_5 k_6 k_7) = k_2 k_3 k_5H(k1k2k3k4k5k6k7)=k2k3k5
2. Direct addressing method
Select a linear function as the hash function
H (K ey) = a ∗ K ey + b H(Key)=a*Key+bH(Key)=aKey+b
3. Folding method Divide
the keyword into several segments with the same number (the last segment may not be enough), and then add them to get the value.
H (k 1 k 2 k 3 k 4 k 5 k 6 k 7) = (k 1 + k 4 + k 7) (k 2 + k 5) (k 3 + k 6) H(k_1 k_2 k_3 k_4 k_5 k_6 k_7)=( k_1+k_4+k_7)( k_2+k_5)( k_3+k_6)H(k1k2k3k4k5k6k7)=(k1+k4+k7)(k2+k5)(k3+k6)
k 1 k 2 k 3 k_1 k_2 k_3k1k2k3
k 4 k 5 k 6 k_4 k_5 k_6 k4k5k6
+ k 7 + k_7 +k7
4. Take the
middle of the square method. After the keyword is squared, select the middle digits as the value.
5. Divide the remainder method and
divide the keyword by PPP remainder obtained as a value, where P is a prime number smaller than the maximum length of the hash table.
H (K ey) = K ey MODPH(Key)=Key \quad MOD \quad PH(Key)=KeyMODP
6. Random number method
Select a random function as the hash function
H (K ey) = random (K ey) H(Key)=random(Key)H(Key)=random(Key)

Two, hash collision

For different keywords, the same value may be obtained through the mapping of the hash function, that is, a conflict has occurred. Here are several ways to solve the hash conflict
1. Public overflow area method
Create a new memory to store the conflicting data
2. Re-detection method
When a conflict occurs, find a new value for the keyword through a function:
V alue = (H (K ey) + di) MODL Value=(H(Key)+d_i) \quad MOD \quad LValue=(H(Key)+di)MODL
whereLLL is the length of the hash table, according todi d_idiThe value of can be subdivided into the following three methods:

  • Linear detection method
    di = 1, 2, …, L − 1 d_i=1, 2, …, L-1di=12L1

  • Square detection method
    di = 1 2, − 1 2, 2 2, − 2 2,… d_i=1^2, -1^2, 2^2, -2^2,...di=12122222

  • Random detection method at
    this time di d_idiTake a set of random numbers

3. Re-hashing
When a conflict occurs, perform the hashing function again. Of course, this hashing function can be the same as the previous one, or it can be different
V alue = RH (H (K ey)) Value=RH(H(Key ))Value=R H ( H ( K e y ) )
4. Chain address method The
chain address method is a method of combining an array with a linked list. This is also the most commonly used method. Different keywords with the same value are stored in a linked list. , Different linked lists, that is, different values ​​form an array.

Three, the use of unordered_map

1. Header file:

#include< unordered_map>

2. Define a hash table (we take Key and Value as int variables as an example):

unordered_map<int,int> Hash;

3. There are several ways to establish a hash table:

Hash[1]=3;
Hash.insert<make_pair(1,3)>;
Hash.insert({ {1,3},{2,4} });

4. Iterator :

unordered_map<int,int>::iterator it;

5. Use iterators to access variables:

it->first;
it->second;

6. Lookup of the hash table:

it=Hash.find(1);

If it is not found, it will return Hash.end();
7. Modify the hash table:

Hash[1] = 4;

8. Clear the hash table:

Hash.erase(1);
Hash.clear();

Above we have introduced several basic usages of hash tables. For detailed introduction, please visit the following website:
https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2008/bb982522( v=vs.90)

Guess you like

Origin blog.csdn.net/Huang_JinXin/article/details/95785544