C ++ related container details-from internal structure to Application

Source: Internet
Author: User

Different from the sequential container, the underlying layer of the ordered container is implemented using arrays and is in a linear structure. The non-linear storage method used in the implementation of the correlated container is used; ordered containers store and access elements sequentially by the position of elements in the container, while associated containers store and read elements through keys. In the c ++ standard template library, the associated containers include set, multiset, map, and multimap. 1. As we have already said, the underlying implementation of the associated container uses a non-linear storage method. What is this non-linear storage method? The answer is "red-black Tree" (RB-Tree), which is a balanced binary Tree with the following features: (1) the value of all left subtree nodes is less than or equal to the value of the root node, and the value of right subtree node is greater than the value of the root node. (2) No node has a large depth. From the above, we can know that the associated container is a specific application that balances Binary Trees, because it is organized through a linked list internally, so it is faster than the vector during insertion, it is slower than list. Because its bottom layer is a balanced binary tree, the time complexity of searching, inserting, and deleting should be O (logN. 2. setset is a set. The elements in the group are unique and arranged in a certain order. Each element can be regarded as a key or a value. 3. The only difference between multisetmultiset and set is that one key can appear multiple times. 4. mapmap has both real values and keys. Each element of mapmap is pair. The first element of pair is the key value, and the second element is the real value. The key is used as the index of the element in the map, and the value indicates the data stored and read. 5. multimapmap does not allow the two elements to have the same key value, while multimap allows repeated key values. 6. the underlying implementation of map and set in the selection of associated containers is very RB-tree. The elements contained in these two types of objects have different keys, the second element cannot be added for the same key. If a key must correspond to multiple instances, you need to use multimap or multiset. These two types allow multiple elements to have the same key. In associated containers, only map supports the following table operations. Generally, If You Want To effectively store a set of different values, it is more appropriate to use the set container, while the map container is more suitable for storing (or even modifying) the values associated with each key.

Related Article

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.