Comparison between map and hash_map in STL

Source: Internet
Author: User

1. map:

In STL of C ++, map is usedTreeSearch Algorithm; time complexity:O (log2N)

 

2. hash_map:

UseHash tableTo sort the pairs. The hash table isUse keywordsTo calculate the table location; time complexity:O (1), The worst time complexity:O (n)


In general
: Hash_map is faster than map, and the search speed is basically unrelated to the data size. It belongs to the constant level, saving a certain amount of memory. If there is no need to sort it, use hash_map whenever possible.

Note:Hash returnTime consumption of hash Functions. When there are million records, map only needs 20 comparisons, and only needs 21 comparisons! So it is not necessarily a constant than log (n); use map or hash_map to weigh the following three aspects:Search speed, data volume, memory usageAnd your experience! No special standards

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.