"C++11" Unoedered_map and map (partial reprint)

Source: Internet
Author: User

1. Conclusion

The new version of the Hash_map are unordered_map, here only unordered_map and map.

Operational Efficiency : The unordered_map is the highest, while map efficiency is low but provides a stable and orderly sequence.

Memory consumption: Map memory consumption is slightly lower, unordered_map memory consumption is slightly higher, and is linearly proportional.

when do you use Which? need unordered container, quick Find Delete, not worry about slightly higher memory with unordered_map; orderly container Stable find delete efficiency, memory is very concerned about time with map.

2. Principle

The internal implementation of map is a two-fork balance tree (red-black tree); Hash_map internal is a hash_table generally by a large vector,vector element node can hook up the list to resolve the conflict, to achieve.

hash_map Its insertion process is:
    1. Get key
    2. Hash value obtained by hash function
    3. Get the bucket number (usually hash value for the number of barrels to die)
    4. Store key and value inside the bucket.
 the procedure for taking the value is:
    1. Get key
    2. Hash value obtained by hash function
    3. Get the bucket number (usually hash value for the number of barrels to die)
    4. Compares whether the inner elements of the bucket are equal to the key, and if they are not equal, they are not found.
    5. Remove the value of the equivalent record.
 The direct address in Hash_map is generated by the hash function, resolving the conflict and solving it with the comparison function. 3. Performance featuresnon-frequent queries with map more stable, frequent queries with hash_map efficiency will be higher, c++11 in the unordered_map query efficiency will be higher, but memory consumption is slightly larger than hash_map. Unordered_map is the HASH_MAP implementation in boost.

In fact, Stl::map corresponds to TreeMap in Java, and boost::unordered_map corresponds to HashMap in Java.
The map in Python is implemented by HASHMAP, so query efficiency is faster than C + + map queries. (Java,python the official version of the virtual machine is implemented in C language, so the internal ideas and methods are universal.) )

if the order is considered, the query speed is stable, the container element quantity is less than 1000, the non-frequent query consider using map.
If a very high-frequency query (more than 100 elements, unordered_map will be faster than map), the internal elements can be non-orderly, the data is larger than 1k or even hundreds of thousands of million times to consider using Unordered_map( Tens of millions of 4GB of memory on the element will worry about insufficient memory, need to move the database stored procedures to disk).
Hash_map compared to unordered_map is tens more than the memory consumption of less than 15MB, hundreds of millions of times memory consumption of 300MB, under million is unordered_map occupy less memory,
and unordered_map Insert Delete is faster than hash_map, the search efficiency is similar to hash_map, or only a little faster than about 1/50 to 1/100.
a combination of non-ordered or stable maps should use the Unordered_map,set type as well.
unordered_map find efficiency five times times faster, insert faster, save a certain amount of memory. If there is no need to sort, try to use Hash_map (Unordered_map is the HASH_MAP implementation in boost).
4. Using Unordered_mapunordered_map requires overloading the Hash_value function and overloading the operator = = operator.
For a detailed reference (thank Orzlzro for writing such a Good article):http://blog.csdn.net/orzlzro/article/details/7099231

"C++11" Unoedered_map and map (partial reprint)

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.