Differences between Hash_Map and Map

Source: Internet
Author: User

1.1 What is the difference between hash_map and map?



Constructor. Hash_map requires the hash function, which is equal to the function; map only needs the comparison function (less than the function ).

Storage Structure. Hash_map uses hash table storage, and map generally uses the red/black Tree (RB Tree. Therefore, the memory data structure is different.

1.2 When do I need hash_map and map?

In general, the hash_map search speed is faster than that of map, and the search speed is basically independent of the data size, which belongs to the constant level, while the map search speed is at the log (n) level. Not necessarily, constants are smaller than log (n), and the time consumption of hash functions is also time-consuming. See, if you consider efficiency, especially when the number of elements reaches a certain order of magnitude, consider hash_map. However, if you are very strict with the memory usage and want the program to consume as little memory as possible, be careful. hash_map may embarrass you, especially when you have many hash_map objects, you cannot control it, and the construction speed of hash_map is slow.

Do you know how to choose? Weigh three factors: search speed, data volume, and memory usage.

Here is a little story about hash_map and map, look at: http://dev.csdn.net/Develop/article/14/14019.shtm

1.3 How to add a custom type to hash_map?

You only need to do two things: Define the hash function and define the comparison function. The following code is an example:

# Include

# Include <string>

# Include <iostream>

 

Using namespace std;

// Define the class

Class ClassA {

       Public:

       ClassA (int a): c_a (){}

       Int getvalue () const {return c_a ;}

       Void setvalue (int a) {c_a = ;}

       Private:

       Int c_a;

};

 

// 1 define the hash function

Struct hash_A {

       Size_t operator () (const class ClassA &
A) const {

               //ReturnHash <int> (classA. getvalue ());

               Return
A. getvalue ();

       }

};

 

// 2 define the equal function

Struct defaults _a {

       Bool operator () (const class ClassA & a1,
Const class ClassA & a2) const {

               ReturnA1.getvalue ()
= A2.getvalue ();

       }

};

 

Int main ()

{

       Hash_map <ClassA, string, hash_A, equal_A>
Hmap;

       ClassA a1 (12 );

       Hmap [a1] = "I am 12 ";

       ClassA a2 (198877 );

       Hmap [a2] = "I am 198877 ";

       

       Cout

       Cout

       Return 0;

}


 

I am 12

I am 198877

1.4 How to replace the existing map container in the program with hash_map?

This is easy, but you need a good programming style. We recommend that you use typedef to define your type as much as possible:

Typedef map <Key, Value> KeyMap;

When you want to use hash_map for replacement, you only need to modify:

Typedef hash_map <Key, Value> KeyMap;

The rest remains unchanged. Of course, you need to check whether there are hash and comparison functions of the Key type.

1.5 Why is hash_map not standard?

I am not sure why it is not standard. I have explained that when STL is added to Standard C ++, The hash_map series were not fully implemented yet and will become a standard in the future. If anyone knows a more reasonable explanation, they also want to tell me. But what I want to express is that it is precisely because hash_map is not a standard, so g ++ compiler is installed on many platforms and does not necessarily implement hash_map. This is an example. Therefore, you must test these non-standard libraries in advance. In addition, it is better to take platform migration into account and use less.

1.6 Are there any suggestions for learning to use hash_map?

Hash Chinese is a hash and also a hash. When you hear other people say that the hash container should not blame yourself for being ignorant. To learn about the hash series, you can also read this article: Negative STL 25: familiar with non-standard hash containers. We also recommend that you check the source code. If you have any questions, you can ask questions on the STL forum.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.