5. STL source code analysis: associated containers

Source: Internet
Author: User
Associated container

The so-called associative container is similar to the relational database in concept: each piece of data has a key value and an actual value ). When an element is inserted into a container, the internal mechanism places the element in a specific position based on the key value according to certain rules. Associated containers do not have the so-called head and tail concept (only the maximum element and the minimum element). Therefore, they do not have operations such as push_back () and push_front.

 

The standard STL associated containers are divided into two categories: Set and map, and the derivative Multiset and multimap. The underlying mechanisms of these containers are implemented using RB-tree, RB-tree is also an independent container and is not open for external use. In addition, sgi stl provides a non-standard associated container hash-table and the hash-set, hash-map, hash-Multiset, hash-multimap.

 

Binary Search Tree

Generally, the internal structure of the correlated container is binary balanced tree, which has many types, including RB-tree, aVL-tree, AA-tree, RB-tree is the most widely used one.

 

The rule of the Binary Search Tree is that the key value of any node is greater than the key value of each node in the left subtree, but less than the key value of each node in the right subtree. The operation time complexity supported by the binary search tree is directly proportional to the height of the tree. For example, if the fruit tree is balanced (in extreme cases, it is a complete binary tree), the operation efficiency is high and unbalanced (in extreme cases, it is a single link ), its operation efficiency is low. To ensure operation efficiency, the so-called balanced binary tree concept exists.

 

A balanced binary tree is a binary search tree that imposes certain conditions to ensure balance. For the definition of AVL trees and RB-TREE and how to ensure balance, see Introduction to algorithms or other books that introduce data structures.

 

Avltree has stricter balance requirements than RB-tree. Why does STL choose RB-tree as the underlying mechanism of the associated container instead of AVL-tree, I think it should be because the algorithm complexity must be increased to ensure the balance itself. The stricter the requirements, the more frequent the tree adjustment during the operation. The RB-tree achieves a better compromise between the two indicators, and the actual effect is better than that of the AVL-tree.

 

Set and mutiset

Set is a set. The key value of its elements is the real value, and the real value is the key value. The two elements cannot have the same value.

 

We cannot change the value of an element through the set iterator. Because the value of a set element is a key value, changing the key value violates the arrangement rules of elements.

 

After the client inserts or deletes the set, the previous iterator is still valid (except for those that are deleted ).

 

STL also provides some set algorithms, including communication and union.

 

The underlying mechanism of set is RB-tree, and all operations are just the behavior of converting RB-tree.

 

The only difference between Multiset and set is that Multiset allows duplicate key values.

Map and multimap

Map elements are pair, the first value is the key value, and the second is the real value.

 

We can use the map iterator to change the real value of the element.

 

Multimap and map are almost the same. The only difference is that repeated key values are allowed.

Hashtable

Hash table can be used to access and delete any famous item. This structure is intended to provide basic operations with constant time.

 

STL hash table uses the chain-based hash method.

 

The storage structure of the hash table is divided into two levels. The first level is the bucket (buckets) of continuous space. Each bucket contains a node linked list. The Bucket List is implemented using STL vector, but the linked list structure has nothing to do with list or slist.

 

The iterator of the hash table does not define the rollback operation.

 

There are many template parameters for STL hash table:

Value: real value type;

Key: key value type;

Hashfun: a function that converts a key value to a hash value (note that this is not a hash function commonly called ing address ).

Extractkey: Method for retrieving key values from a node

Primary Key: A function used to determine whether the key value is equal.

Alloc: Space configurator, default STD: alloc

 

Although the open link method does not require the table size to be a prime number, STL uses a prime number. Then, prepare 28 prime numbers for access at any time. When you need to initialize or expand a table to N, set the actual size of the table to the nearest and not less than N.

 

The condition for STL hash table to expand a table is that when the number of elements is greater than or equal to the table size. (This condition should be obtained based on statistics to ensure constant operation time ).

Hash_map, hash_set, hash_multiset, hash_multimap

These containers correspond to the one-to-one mechanism described above, but these are implemented at the underlying level using hash_tabel.

The underlying mechanism determines the differences between the two groups of containers:

The RB-tree group sorts elements, but the hash_map group does not;

The time complexity of Rb-tree search is lg (N), while the hash_map group is a constant time;

The RB-tree group does not waste nodes in space utilization, while the hash_map group may have some vacant buckets.

 

 

 

 

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.