Introduction to unordered series of associative containers

In C++98, STL provides a series of associative containers whose bottom layer is a red-black tree structure, and the query efficiency can reach log 2 N log_2 Nlog2N , that is, in the worst case, it is necessary to compare the height times of the red-black tree. When there are many nodes in the tree, the query efficiency is not ideal. The best query is to find elements with a small number of comparisons, so in C++11, STL provides 4 unordered series of associative containers.

An associative container of the unordered series

unordered_map、unordered_set、unordered_multimap、unordered_multiset

Next, we will talk about unordered map and set, which is to improve search efficiency.

Compared with ordered containers, these 4 unordered containers have higher search efficiency; second, they do not support reverse iterators, only support ++, and do not support –; third, the ordered bottom layer With a red-black tree, when the bottom layer of STL uses a hash table to implement an unordered container, all data will be stored in a whole continuous memory space, and when the data storage location conflicts, the solution is to use the "chain address method". " (Also known as "open chain method").

The understanding of key-value pairs: recorded as key: value (key: value). The key is the index, and the value is the data. key is unique.

unordered container Function
unordered_map Store elements of the key-value pair <key, value> type, where the key of each key-value pair is not allowed to be repeated, and the key-value pairs stored in the container are unordered
unordered_multimap The only difference from unordered_map is that this container allows storing multiple key-value pairs with the same key
unordered_set Data is no longer stored in the form of key-value pairs, but directly stores the data elements themselves (of course, it can also be understood that all the containers store are key-value pairs with the same key and value. Because they are equal, only Just store the value). In addition, the elements stored in the container cannot be repeated, and the elements stored in the container are also out of order
unordered_multiset The only difference from unordered_set is that this container allows elements with the same value to be stored

unordered_set

The data is out of order, data redundancy is not allowed, and a hash load factor adjustment function is provided, and other functions are similar to ordered set

unordered_map

Data is unordered, data redundancy is allowed, bucket operation related functions and hash load factor adjustment functions are provided, and other functions are similar to ordered set. unordered_multimap does not support operator[] operations.

operator[key] function

Returns the value corresponding to key, there is no default value. Note: This function actually calls the insertion operation of the hash bucket, and uses the parameters key and V() to construct a default value and insert it into the underlying hash bucket. If the key is not in the hash bucket, the insertion is successful, return V(), and insert If it fails, it means that the key is already in the hash bucket, and the value corresponding to the key will be returned.

bucket operation function

function declaration Function
size_t bucket_count()const Returns the total number of buckets in the hash bucket
size_t bucket_size(size_t n)const Returns the total number of valid elements in bucket n
size_t bucket(const K& key) Returns the bucket number where the element key is located

Compare

For the insert of unordered data, the unordered series is more efficient; for the insert of ordered data, the order series is more efficient. ==> The comparison of insertion is not obvious. The efficiency of the two is different when the amount of data is different (ten thousand, million) and the environment is different (windows, linux).

For the erase of unordered data, the unordered series is more efficient; for the erase of ordered data, the order series is more efficient. ==> The comparison of deletion is not obvious. The efficiency of the two is different when the amount of data is different (ten thousand, million) and the environment is different (windows, linux).

For the find of data, the unordered series is more efficient. ==> The comparison of find is obvious, the search efficiency of the unordered series is extremely high!

The advantage of the unorder series is the find function.

There is only one difference from an ordered container, an unordered container does not sort the stored key-value pairs. In actual scenarios, if a large number of operations involving traversing containers are involved, it is recommended to prefer associative containers; on the contrary, if more operations are to obtain corresponding values ​​through keys, unordered containers should be preferred.

Guess you like

Origin blog.csdn.net/m0_61780496/article/details/129681603