Differences between C ++ hash_map and Java HashMap

Source: Internet
Author: User

Compared with HashMap in Java, the implementation of hash_map in C ++ STL is not perfect. The reason is that there are some small details:

1. Hash Table growth mode:

Hash_map: when the number of elements element_NUM exceeds the number of buckets bucket_NUM, determine the number of new buckets N. N is a prime number, which is selected from a prime number table. It must be greater than or equal to the number of elements element_NUM. In a prime number table, the prime numbers are arranged from small to large. Basically, the latter is twice the former.

HashMap: the load factor is used. We know that the load factor = number of elements/number of buckets. The load factor here is 0.75. There is also a concept of threshold. The threshold = the number of buckets * the load factor, that is, the maximum number of elements that can be accommodated under the current number of buckets. When the number of elements exceeds the threshold, the bucket is expanded. During each expansion, the number of buckets is doubled. The number of buckets used here is not a prime number, but an integer power of 2.

Generally, when the number of elements is the same, the number of HashMap buckets is greater than that of hash_map. Therefore, the former has fewer conflicts.

Because the number of buckets used is different, there are 2nd differences.


2. Map the hash value hash_code to the bucket index bucket_index:

Hash_map: modulo operation, bucket_index = hash_code % Number of buckets.

HashMap: bitwise operation, bucket_index = hash_code & (number of buckets-1 ).


3. Use the string hash function:

All are empirical hashing functions.

Hash_map:

  inline size_t __stl_hash_string(const char* __s)  {                  unsigned long __h = 0;          for ( ; *__s; ++__s)         __h = 5*__h + *__s;          return size_t(__h);  }

HashMap: replace 5 with 31.

In my opinion, the hash function DJB is more uniform.


Appendix: combined with the C ++ HashMap transformed by the HashMap of Java, it is included in the resources I uploaded.




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.