STL source code analysis-sequence container and stl source code analysis
STL source code profiling-sequence container
For containers in STL, there is a certain inclusion relationship. For example, heap contains a vector, priority-queue contains a sample, and stack and queue both contain a deque, set/map/multiset/multimap all contain a RB-tree, And hash_x all contain a hashtable.
For a sequence container, insertion of vector and list is performed before pointing to the iterator. At the same time, it should be noted that for vector, calling the erase () function is to clear several elements in the continuous space and call its destructor, the memory occupied by the original element is not released. Even the clear function does not release the memory, but only clears all elements in the original vector. The memory will be released only when the vector destructor is created. However, this is different from list. For List, erase deletes a node, when clear is called, all nodes in the List are deleted, and the List memory is released. This is a little different from the last one.
In addition, for List, a special feature is the transfer function, which can be used to migrate nodes in a certain range to the front of a certain node. At the same time, the [first, last) range received by transfer can also be the same list. However, transfer is not a public interface, and list public provides so-called joint operation splice.
At the same time, the list container also provides reverse () sort () merge () functions. In fact, these functions are also provided in the algorithm library, but they are also mentioned in objective STL, if a function is defined inside the container, it is best to use the member function inside the container.
Deque is a bidirectional open continuous linear space. You can insert and delete elements at the beginning and end. Deque allows you to insert or remove elements from the start end within a constant period. Deque does not have the concept of capacity, because it is a dynamic combination of piecewise continuous space, can add a new space and link up at any time. For example, if the space of a vector is insufficient, re-configuring a larger space, copying elements, and releasing the old space won't happen in deque. Deque also provides random access iterator, but its iterator is not a common pointer and sorts deque. To improve efficiency, you can copy deque to a vector first, after sorting the vector, copy it back to deque.
To understand that the cost of vector capacity expansion is very high, we need to apply for a larger space, reset the data, and destroy the original data. Deque is composed of a certain segment of quantitative continuous space. Once it is necessary to add new space at the front or end of deque, a quantitative continuous space is configured and connected to the entire deque header or tail. You must understand that deque provides the same iterator type as vector, so it is very troublesome internally. Deque is an internal pointer array, and the elements are memory segments. In fact, all operations of deque's external encapsulation are similar to those of vector, but internal operations are complicated. The clear function in Deque also has only one buffer zone, and all data is destroyed, returning to the initial state of deque.
Stack is not a container. It is more like an adapter. the inbound and outbound elements of the stack must comply with the "first-in-first-out" condition. Only the elements at the top of the stack can be used by the outside world. By default, deque is used at the underlying layer of the stack. Stack provides an entry to the underlying implementation mechanism. By default, deque is used.
Queue is a data structure that is first-in-first-out. By default, the queue uses the deque underlying structure. In fact, using list can also be used as the underlying structure of queue and stack. Queue and stack are not counted as containers but as the adapter type. Deuque is used by default at the underlying layer.
Heap is not an STL component, but a helper for priorityqueue.
If the underlying mechanism of the highest priority queue of the Binary Search Tree is used, the insertion and deletion of elements are at the Log level, which is a major concern. Because the input of the Binary Search Tree is random and not easy to implement, the underlying implementation of the final priority queue selects a full binary tree, that is, a binary heap. At the same time, Arrays can be used on the right side of the full binary tree storage. In this way, an array and a heap algorithm are used to insert and delete elements, take the extreme values, and arrange an array into a heap, in the end, the maximum or minimum heap is used to implement the priority queue, and heap algorithm operations are used. Vector is used for storage. For the heap algorithm, push_heap pop_heap sort_heap make_heap only has these four functions.
However, these four functions are very important in constructing priority queues. By default, the underlying layer of Priority Queues uses vector to store elements, but heap functions are used to process stored data. Externally encapsulated functions include:
The maximum heap used by default by the constructor priority_queue empty () push () pop () top.
The default linked list in STL is a two-way linked list, and a one-way linked list slist is also provided. the main difference between slist and list is that the former iterator is one-way forward iteratro and the latter is two-way bidirectional iterator. Their insert, remove, and other operations will not invalidate the original iterator. One thing to note is that for inserting vector and list, the elements are inserted before the specified iterator. But it is a little difficult for slist. It must be retrieved from the beginning, so it is not suitable to use the insert or erase operation functions in other locations except the regions near the slist start point. Slist provides Insert_after () and erase_after ().
Note that the slist implemented here is the same as the kernel implementation. To put it bluntly, the list in STL is a two-way linked list with header nodes. For chained storage (including deque), all the memory is destroyed after the clear function is called, swap function is also implemented for list. Push_front () pop_front () is also provided ().
Slist is also the next pointer of the head returned by begin () with a header node.
Summary: For sequence containers, STL implements several standard containers and also implements several adapters. The containers include the vector list deque slist (non-standard) adapter and the stack queue priority_queue
Vector is a list of consecutive Memory Spaces, which is a two-way linked list. deque is complicated, but stack and queue are implemented by deque by default. Although the Priority_queue Storage Element uses a vector, the corresponding operations are related to heap.
For a function, the special one is list. Because of its particularity, it implements a lot of functions in the original algorithm library, such as sort merge remove. Note that the clear function of deque list slist is unusual.