Why need Hash_map
Use the map bar. Map provides a very common feature that provides key-value storage and lookup capabilities. For example, I want to record a person name and corresponding storage, and always add, to quickly find and modify:
Yeu Bu-Hua Shan faction head, called Gentleman Sword
Zhang-Wudang, founder of Taijiquan
East undefeated-the first master, Sunflower treasure
...
This information, if preserved, is not complicated, but it is difficult to find. For example, I am looking for "Zhang San Feng" information, the stupidest way is to get all the records, and then by the name of a comparison. If you want to be fast, you need to put these records in alphabetical order, and then look for them in the second division. But when you add records, you need to keep records in order, so you need to insert a sort. Given the efficiency, this requires a two-fork tree. It's going to go on and on, if you use the STL map container, you can easily implement this feature without worrying about the details. About map data structure details, interested friends can see Learning STL map, STL set of data structure basis. Look at the implementation of the map:
#include <map>
#include <string>
using namespace Std;
...
Map<string, string> Namemap;
Increase...
namemap["Yue not Group"]= "Huashan faction boss, called Gentleman Sword";
namemap["Zhang San Feng"]= "Wudang Head, the founder of Taijiquan";
namemap["Oriental Undefeated"]= "the first master, Sunflower Treasure";
...
Find..
if (Namemap.find ("Yue Qun")!= Namemap.end ()) {
...
}
I don't think it's easy to use. And the efficiency is very high, 1 million records, at most also as long as 20 times String.Compare comparison, can find you to look for the record, 2 million records matter, also only uses 21 times the comparison.
Speed will never meet the needs of the reality. If there are 1 million records, I need to search frequently, 20 times will be a bottleneck, if it can be reduced to a single or two comparisons is possible. And when the number of records to 2 million is also one or two times comparison, whether it is possible. It also needs to be as easy to use as the map.
The answer is yes. Then you need to has_map. Although Hash_map is not currently included in the C + + Standard Template Library, almost every version of the STL provides a corresponding implementation. And it is widely used. Before the formal use of hash_map, first look at the principle of hash_map.
1 data structure: hash_map principle
This is a section that gives you an in-depth understanding of hash_map, if you just want to be swallowed, don't want to understand the principle, you can skip this section, but I suggest you look at it, it doesn't hurt to know more.
Hash_map is based on a hash table (hash table). The biggest advantage of hash table is that the time of storing and searching data is greatly reduced, which is almost regarded as constant time, and the cost is only more memory. However, in the current more and more available memory, it is worthwhile to use space for time. In addition, coding is also one of the characteristics of its relatively easy.
The basic principle is to use an array with a subscript range that is larger to store the elements. You can design a function (hash function, also called a hash function, so that the keywords for each element correspond to a function value (that is, an array subscript, a hash value), which is used to store the element by using the array element, or it can be simply understood to "classify" each element according to the keyword, The element is then stored in the corresponding "class" where it is called a bucket.
However, it is not possible to guarantee that the keyword of each element corresponds to a function value of one by one. So it is very likely that the same function values are computed for different elements, thus creating a "conflict", in other words, to separate elements into the same "class". In general, "direct addressing" and "conflict resolution" are two major features of a hash table.
Hash_map, first allocate a large amount of memory, forming many barrels. is to use the hash function, the key is mapped to different regions (barrels) to save. The insertion process is:
1. Get key
2. Hash value obtained by hash function
3. Get the bucket number (generally for the hash value of the bucket number of modulo)
4. Store key and value in bucket.
Its value process is:
1. Get key
2. Hash value obtained by hash function
3. Get the bucket number (generally for the hash value of the bucket number of modulo)
4. Compare whether the inner element of the barrel is equal to key, and if it is not equal, it is not found.
5. Take out the value of an equal record.
In Hash_map, the direct address is generated by hash function, which solves the conflict and solves it by comparison function. As you can see here, if there is only one element inside each bucket, then there is only one comparison for the lookup. Many queries are faster when there is no value in many barrels (when they are not found).
Thus, to achieve a hash table, and user-related is: hash function and comparison function. These two parameters are exactly the parameters we need to specify when using Hash_map.
2 Hash_map Use
2.1 A simple example
Do not worry how to "Yue not group" with Hash_map said, we first look at a simple example: random to give you an ID number and ID number corresponding to the information, ID number of the range is 1~2 31 times. How to quickly save a lookup.
#include
#include <string>
using namespace Std;
int main () {
Hash_map<int, string> Mymap;
mymap[9527]= "Tong Pak Fu Point Chou Xiang";
Mymap[1000000]= "The Life of a Millionaire";
mymap[10000]= "The bottom line of white-collar wages";
...
if (Mymap.find (10000)!= Mymap.end ()) {
...
}
Simple enough to use the same method as the map. Then you might ask. hash function and comparison function. Not to be specified. You're right, but when you don't specify the hash function and the comparison function, you have a default function, and you'll see the HASH_MAP statement. Here is the SGI STL statement:
Template <class _key, class _tp, class _HASHFCN = Hash<_key>
Class _equalkey = Equal_to<_key>,
Class _alloc = __stl_default_allocator (_TP) >
Class Hash_map
{
...
}
In other words, in the example above, there are the following equivalent relationships:
Alloc we should not take too much attention (want to know more about allocator's friends can see the standard library stl:allocator can do)
hash function of 2.2 hash_map
hash< int> exactly what it looks like. Look at the Source:
In other words, if your key is using one of the above types, you can use the default hash function. Of course, you can also define your own hash function. For a custom variable, you can only do so, for example, for string, you must customize the hash function. For example:
struct str_hash{
size_t operator () (const string& str) const
{
unsigned long __h = 0;
for (size_t i = 0; i < str.size (); i + +)
__h = 5*__h + str[i];
Return size_t (__h);
}
};
If you want to take advantage of the system-defined string hash function, you can write this:
struct str_hash{
size_t operator () (const string& str) const
{
return return __stl_hash_string (Str.c_str ());
}
};
Note the following points when declaring your own hash function:
1. Use struct, and then overload operator ().
2. Return is size_t
3. The parameter is the type of key you want to hash.
4. The function is a const type.
If these are more difficult to remember, the simplest way is to suit, find a function to change it.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service