STL Source Code Anatomy---associative container

Source: Internet
Author: User

STL Source Code Anatomy---associative container

Standard associative containers are divided into set and map categories, including Multiset and Multimap, the underlying mechanisms of these containers are rb-tree. The association container outside the standard has Hashtable and the hash_set that is done with this hash table as the underlying mechanism ( Hash set) Hash_map (hash mapping table) Hash_multiset Hash_multimap.

The internal relationships of sequences and associative containers are included, for example the heap contains a vector,priority_quehe containing a heap,stack and a queue containing a deque,set/map/multiset/ Multimap contains a rb-tree,hash_x that contains a hashtable

When an element is inserted into an associative container, the internal structure of the container is placed in the appropriate position according to the size of its key value, in a particular rule. Associative containers have no so-called Kinsoku, only the largest and smallest elements.

Binary search tree Specifies that the key value of any node must be greater than the key value of each node in its left subtree, and is less than the key value of each node in its right child tree.

Avl-tree, the binary balance tree adjusts the rules, if a certain subtree balance is broken, then depending on the position of the newly inserted node can be divided into the following: (x is the root node of the subtree that is breaking the balance)

Zuozi of the insertion point at the left child node of X-left left

The insertion point is in the right subtree of the left child node of X-Around

Zuozi-right-left of the insertion point at the right child node of X

The insertion point is in the right subtree of the right child node of X-Right-right

For 1, 4 symmetrical to each other, become an external insert, can be adjusted with a single rotation operation, for 2, 3, called the inner Insert, you can use double rotation operation.

On the red and black trees, let's look at a few questions, first of all, for the red and black trees have a few rules, any one node is not red or black, with the node is black, any node to null (tree end) Any path, the number of black nodes must be the same, if the node is red, its child nodes must be black.

According to the above rules, then we have to insert a new node must be red, assuming that the insertion node is x, the father is P, grandfather is G, great grandfather for GG, Uncle node for S.

The inserted node x must be a leaf node, if the father of X is black, then without adjustment, if the father of X is red, then it must be adjusted. If P is red, then G must be black. This time only depends on the position of X and the color of GG and s to decide how to adjust the next step.

This makes it clear why the S-node is the precondition for the adjustment of the red-black tree. It must be clear that the P must be red. Then know that in the adjustment is to use s to adjust the rules are good to do, to determine whether S is red or black.

If S is red, while P is red and g is black, it is easier to see if x is inserted outside or inside.

If S is black, at the same time P is red, G is black, this time need to see whether GG is red or black, these two cases are more complex, especially when GG is red.

The need to adjust the premise (P is red) can be divided into two cases according to S, while in each case can be divided into two situations. This explains why the node color of S is divided when the red-black tree is inserted.

(Note: If you insert a new node x in a red-black tree, the node color must be red, then why is it judged by the S-node?) First understand and x related to all have what node, have p G GG S, if P is black, then do not adjust, if p is red, then must adjust, and G is black, then the related only s and GG, this time will need to judge the next how to adjust by S. Although this is said in the theoretical analysis, but the implementation of the program may not be written according to the theory of the program, it may be necessary to determine whether the Father node is the left node or the right node.

In the STL in the red black tree insert operation has the relatively novel method, here assumes that inserts the new node is a, then walks up along A's path, as long as sees has a node X's two sub-nodes all is red, then changes the X to red, and the two child node changes the black. After this adjustment, if A's parent node p is also red (note that at this time S is not likely to be red, for what, because if after the above adjustment p is still red, while S is also red, then according to the rules of adjustment soon to adjust the sub-node), do a single rotation and change the color, or double rotation retreat to change color. Inserting the node again is simple, either by inserting it directly or by inserting it and then rotating it again. This process is different from the usual red-black tree insert operation.

The feature of set is that all elements are automatically sorted based on the key value of the element. Set elements do not have the same real and key values as map, the key value of the set element is the real value, and the real value is the key value. Set does not allow two elements and the same key value.

At the same time, the set element value cannot be changed by the set iterator, which is also introduced in Effectivestl, because the set element value is its key value, which relates to the arrangement rules of the set element. If the set element value is arbitrarily changed, the set organization is severely destroyed, and set<t>::iterator is defined as the const_iterator of the underlying rb-tree, eliminating the write operation.

In the case of associative containers, it is more efficient to search for elements using the Find function provided by itself, rather than using the STL algorithm find (), because the STL algorithm find () is only a sequential search

The map attribute is that all elements are automatically sorted based on the key value of the element, all the elements of the map are pair, with both real and key values, the first element of the pair is treated as a key value, and the second element is considered a real value. Map does not allow two elements to have the same key value. Is it possible to change the element content of a map via the map's iterator? If you want to fix the key value of an element, it is not, because the key value of the map element is related to the collation of the map element. Modifying the Map element key values destroys the map organization and is possible if you want to modify the real value of the element.

Both the map and the set have their own find () functions. As for Multiset and Multimap, the insert_equal () function in Rb-tree is used to indicate that the key value can be duplicated.

As for Hashtable, a vector is used internally as a bucket, and the list is maintained within each bucket. There are quite a few parameters for the Hashtable template. One thing Hashtable need to be aware of is the customization of several ready-made hash functions, all of which are functor functions. Hashtable there are some types that cannot be processed (unless the user customizes the hash funcitons for those types), the hash_set cannot handle the hashtable. The swap function is defined for the associated container using Hashtable as the underlying mechanism. Also has its own find () function.

For Hash_sethash_multiset, the template parameter uses up to three, a value, a function, and a judgment equality function. Hash_set uses a method similar to set. Hash_multiset features are the same as multiset, and the key underlying implementations are different, and hash_multiset elements are not automatically sorted.

For hash_maphash_multimap template parameter customization there are up to four, a key value, a value, a hash function, and a judgment equality function. Because Hash_map stores the pair, it is similar to how map is used.

Conclusion: The associative containers are divided into two categories, one is Set/map/multiset/multimap, the other is hash_x/hash_multi*, the former is based on the red and black tree as the underlying mechanism, the latter is the underlying mechanism according to Hashtable. So the former is inherently automatically sorted, and the latter does not have a sort function. It is also necessary to understand that some functions are implemented within the associative container, and in conflict with some algorithms in the algorithm library, the internal implementation functions are used as much as possible. There is also the problem of modifying the inner elements of the container through iterators, and there is a difference between set and map.

STL Source Code Anatomy---associative container

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.