Brief analysis of C++stl container

Source: Internet
Author: User

Standard STL sequence Container: Vector, String, deque, and list.
Standard STL Associative containers: Set, Multiset, map, and Multimap.
Non-standard associative containers hash_set, Hase_multiset, Hash_map, and Hash_multimap.

(1) Vector container
Vector data is arranged and manipulated in a very similar way to array. The only difference between the two is the flexibility of the use of space. An array is a static space that cannot be changed once it is configured. Vector is a dynamic space, with the addition of elements, its internal mechanism will expand its own space to accommodate the new elements. Therefore, the use of vectors for the rational use of memory and the flexibility of the use of a lot of help, we no longer because of the fear of lack of space and began to ask for a large array.

Vector dynamic increase in size, not in the original space after the continuous new space (because there is no guarantee of space after the original space), but instead of twice times the original size of a larger space, and then copy the original content, and then began to construct new elements after the original content, and release the original space. Therefore, any action on the vector, once the space is reconfigured, is invalidated by all iterators pointing to the original vector.

(2) List container
The list becomes much more complex relative to the vector's contiguous space, with the benefit of configuring or releasing an element space each time an element is inserted or deleted. Therefore, the list of the use of space is absolutely accurate, not a waste. Also, for element insertion or element removal at any location, the list is always constant time. The list in the STL is a doubly linked list, and it is a circular doubly linked list.

(3) Deque container
Deque is a continuous linear space with bidirectional openings. The so-called two-way openings, meaning can be at the end of the team at each end of the insertion and deletion of elements. One of the biggest differences between deque and vectors is that deque allows the insertion or removal of elements into the head at constant time, and the other is that deque does not have a concept of capacity, because it is dynamically composed of segmented contiguous spaces that can be added to a new space at any time and linked together. In other words, things like vectors that "reconfigure a larger space with insufficient old space, then copy elements, and then release the old space" are not going to happen in deque.

Deque is composed of a period of quantitative continuous space. Once it is necessary to add new space at the front or end of the deque, a fixed amount of continuous space is configured to thread the head end or end of the entire deque. The biggest task of deque is to maintain the illusion of its overall continuity in the quantitative continuous space of these segments, and to provide a random access interface. Avoid the "Reconfigure, copy, release" cycle, the cost is a complex iterator architecture. Because there is piecewise continuous linear space, there must be a central control, and in order to maintain the illusion of overall continuity, the design of the data structure and the iterative forward and backward operations are quite cumbersome.

Deque uses a so-called map as the main control. The map here is a small contiguous space where each element is a pointer to another contiguous linear space called a buffer. The buffer is the storage space body of the deque. SGI STL allows us to specify the buffer size, and the default value of 0 means that the bytes buffer will be used.

(iv) Stack
A stack is a data structure that is advanced and out (first-in-last-out, FILO). It has only one exit, and the stack allows new elements to be removed, removing elements and getting the topmost element. However, there is no other way to access other elements of the stack except at the top, and the stack does not allow traversal behavior.

With some kind of container as the bottom structure, it is easy to change its interface so that it conforms to the characteristic of "advanced out" and forms a stack. Deque is a two-way open data structure, if the deque is the bottom structure and closed its head end of the opening, it is easy to form a stack. Therefore, SGI STL will use the deque as the bottom structure of the stack by default, since the stack system completes all its work with the bottom container. And with this "modify something interface, form another style" of nature, called adapter (adapter), therefore, STL stack is often not classified as container (container), and is classified as container adapter.

(v) Queue
A queue is a data structure that is a first-in Out,fifo. It has two exits, and the queue allows adding elements, removing elements, and adding elements from the bottom to get the topmost element. But in addition to the bottom can be added, the top can be taken out, there is no other way to access the other elements of the queue.

With some kind of container as the bottom structure, it is easy to change its interface so that it conforms to the "FIFO" feature and form a queue. Deque is a two-way open data structure, if the deque as the bottom of the structure and closed its bottom of the exit and the front entrance, it is easy to form a queue. Therefore, the SGI STL will use deque as the default queue bottom structure because the queue With the bottom container to do all of its work, and with this "modify something interface, form another style" of nature, called adapter (adapter), therefore, the STL queue is often not classified as container (container), and is classified as container adapter.

(vi) heap
Heap does not belong to the STL container component, it is a behind-the-scenes hero who plays the priority queue Assistant. The priority queue allows the user to push any element into the container in any order, but it must be taken from the element with the highest precedence at the time of removal. According to the arrangement of the elements, the heap can be divided into max-heap and Min-heap two, the former each node key value (key) is greater than or equal to its child node key value, the latter of each node key value (key) is less than or equal to its child node key value. Therefore, the maximum value of the max-heap is at the root node and always at the beginning of the underlying array or vector, and the minimum value of the min-heap is at the root node and always at the beginning of the underlying array or vector. STL supplies the max-heap, which is implemented in C + +.
Heap Sort C language implementation
Http://www.cppblog.com/tankzhouqiang/archive/2011/03/21/142413.html

(vii) Priority_queue
Priority_queue is a queue of weighted values that allows new elements to be added, remove old elements, and examine elements such as values. Since this is a queue, it is only possible to add elements at the bottom and remove elements from the top, and there is no other way to access the elements. Priority_queue with the concept of values, the elements are not in accordance with the order in which they are pushed, but automatically according to the weight of the elements (usually the weights are expressed in real value). The highest of the weights, ranked at the front. By default, the priority_queue is done using a max-heap, which is a vector-based complete binary tree.max-heap that satisfies the priority_queue required " The character of automatic descending order of weights and lows.
The priority_queue is completely based on the bottom container, plus the heap processing rules, so its implementation is straightforward. By default, vectors are the bottom container. The queue completes all its work with the bottom container. With this "modify something interface, form another style" of nature, called adapter (adapter), therefore, STL priority_queue is often not classified as container (container), and is categorized as container adapter.

(eight) Set,multiset
The feature of set is that all elements are automatically sorted based on the key value of the element. The set element does not have a real value (value) and a key value (key) as the map does, and the key value of the set element is the real value, the real value is the key value, and set does not allow two elements to have the same value. Set is achieved by the red black tree, because the red black tree (rb-tree) is a balanced binary search tree, the effect of automatic sorting is very good, so the standard STL set is rb-tree as the underlying mechanism. And because of the various operating interfaces opened by set, Rb-tree also provides, so almost all set operation behavior, only call Rb-tree operation behavior.

The multiset features and usage and set are identical, the only difference being that it allows the key value to be duplicated, so its insert operation is based on the underlying mechanism Rb-tree insert_equal () rather than Insert_unique ().

(ix) Map,multimap
The feature of map is that all elements are automatically sorted based on the key value of the element. All elements of the map are pair, with both a real value (value) and a key value (key). 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. Since rb-tree is a balanced binary search tree, the effect of automatic sorting is very good, so the standard STL map is rb-tree as the underlying mechanism. And because of the various operating interfaces opened by map, Rb-tree also provides, so almost all map operation behavior, is only the rb-tree operation behavior.
The Multimap feature and usage are exactly the same as the map, except that it allows the key value to be duplicated, so its insert operation is based on the underlying mechanism Rb-tree insert_equal () rather than insert_unique.

Brief analysis of C++stl container

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.