Key Points of STL source code analysis, stl source code analysis

Source: Internet
Author: User

Key Points of STL source code analysis, stl source code analysis
1. STL space Configurator

Sgi stl has designed a dual-layer configurator. The first-level configurator directly uses malloc () and free (). The second-level configurator uses different policies as needed: when the configuration block exceeds bytes, it is considered as "large enough" to call the first-level configurator. When the configuration block is smaller than bytes, it is considered as "too small" to reduce the extra burden, the complex memory pool sorting method is adopted, and the first-level Configurator is no longer used. The second-level configurator maintains 16 free linked lists and is responsible for the secondary configuration of 16 small blocks. The memory pool is configured with malloc. If the memory is insufficient, call the first-level configurator.

It is the responsibility of the client to design the "memory insufficiency processing routine. If the client is not designed, STL will directly throw the bad_alloc exception or exit the program using exit (1.

When the second-level configurator configures a block, the memory demand for the small block to be configured is raised to a multiple of 8. When no block is available in the free list, refill () is called. By default, 20 new nodes are obtained.

2. STL iterator

Iterator: acts as the glue between containers and algorithms. It is a so-called "Generic pointer". The iterator must implement operator *, operator->, operator ++, operator --. Because only the container designer knows how to traverse its own elements, STL does not list the iterator separately to satisfy all containers.

Partial Specialization (Partial Specialization) means that if the class template has more than one template parameter, you can perform special work on one or more template parameters. (Another explanation is a special version designed for any further condition limitations of the template parameter ).

Quick Sort generic algorithms must require that the iterator be RandomAccessIterators. Any element can be selected as a pivot, but its suitability may affect the efficiency of Quick Sort. In order to avoid the deterioration effect caused by not random pivot, the ideal method is to take the elements at the beginning, end, and center of the entire sequence, and use the values as the pivot, this method becomes Median-of-Three. to quickly retrieve the elements at the central position, it is clear that the iterator must be able to locate them randomly, therefore, the iterator in the generic Algorithm for fast sorting must be RandomAccessIterators.

3. Sequential containers

Vector maintains a continuous linear space (which can be understood as an array) and supports random access. Therefore, the provided iterator is RandomAccessIterators. When a new element is added, if the current capacity is exceeded, the capacity will be doubled. The so-called dynamic increase is not to add a new space after the original space, but to configure another space twice the original size, and then copy the original content, then, a new element is constructed after the original content and the original space is released. Therefore, any operations on the vector, once space reconfiguration occurs, all iterators pointing to the original vector will become invalid.

List encapsulates the linked list. The sgi stl list is a two-way linked list (double linked-list). The Iterator must be able to move forward and backward. Therefore, the list provides Bidirectional Iterator (note, list does not support random access, so it does not provide RandomAccessIterators ). Only the iterator that points to the deleted element in the list deletion operation fails, and other iterators are not affected.

The main difference between list and vector is that vector uses continuous memory storage. It supports the [] operator, while list is implemented in the form of a linked list and does not support []. Vector is fast for random access, but it is slow for inserting elements, especially in the header, and fast for inserting elements at the end. List is much slower for random access because it may be possible to traverse the entire linked list, but it is much faster to insert, and data does not need to be copied or moved, you only need to change the pointer point. In addition, vector has a set of algorithms for newly added elements, and list can be added as needed.

Vector is the continuous linear space of one-way open, and deque is a continuous linear space of two-way open. When space is insufficient, a vector allocates another space, copies the original content, and deletes the old space. While deque allocates another space, the deque interface provides a continuous interface, obviously, the deque internal iterator has done a lot of work, making the deque at least seemingly continuous and linear.

Stack is the encapsulated deque. In other words, stack is actually an adapter. The adapter encapsulates some interfaces of internal objects and does not provide access to other interfaces of internal objects. It is obviously different from inheritance.

4. Associated containers

Map, set is a standard associated container and uses a very efficient balanced binary tree for search: red/Black (RB-tree ), its insertion and deletion efficiency is higher than that of other serial containers because it does not need to perform memory copy and memory movement, but directly replaces the pointer pointing to the node. The balanced binary tree has many types, including AVL-tree and RB-tree.

Binary search tree, also known as binary search tree, provides element insertion and access to log time. The key value of any node must be greater than the key value of each node in the left subtree and smaller than the key value of each node in the right subtree. Therefore, you can go from the root node to the left until there is no left path. Remember the smallest element. On the contrary, you can obtain the largest element.

Note: insertion and deletion of the binary search tree are usually used as interview questions. There are two types of delete operations: one is that the deleted node has only one subnode, And you can directly replace this subnode with the parent node of the deleted node; if both the left and right subnodes of the deleted node exist, replace the minimum value in the right subtree with the deleted node.

Set automatic sorting. You cannot directly modify elements. You need to delete the elements before inserting them. It cannot be repeated. multiset can. After the set element is inserted and deleted, the original iterator except the node iterator to be deleted does not expire. The reason why set cannot directly modify the element value is that the element value of set is its key value, which is related to the Automatic Sorting rule of the set element.

Map Automatically sorts elements based on their key values. All the elements of map are pair and have both the key and the real value ). The first element of pair is called a key value, and the second element is called a real value. The map key value cannot be modified. Map does not allow two elements to have the same key value.

The difference between set and vector is that set does not contain duplicate data. The difference between set and map is that set only contains keys, while map has two elements, value corresponding to a key and a key. The difference between map and hash_map is that hash_map uses the hash algorithm to speed up the search process, but requires more memory to store these hash elements. Therefore, it can be considered that space is used in exchange for time policies.

 

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.