The implementation principles of stl are simple and easy to understand, and stl principles are explained.

Source: Internet
Author: User

The implementation principles of stl are simple and easy to understand, and stl principles are explained.

Summary
Please use vector for frequent Random Access
2. list
A list is a two-way linked list, where elements are stored in the heap. Each element is stored in a piece of memory. Its memory space can be discontinuous and data access can be performed through pointers, this feature makes random access very inefficient, so it does not provide a [] OPERATOR overload. However, due to the characteristics of the linked list, it supports deletion and insertion anywhere with good efficiency.
List has no space reserved habits, so every allocation of an element will be allocated from the memory, Each deletion of an element will release the memory it occupies.
The performance of adding and deleting elements to a list is high, and the memory does not need to be moved. Of course, every element does not need to be constructed or parsed. Therefore, it is often used as a random operation container. however, when accessing the elements in the list, the first and last access is the fastest.
The other elements to be accessed are O (n), So If frequent random access is required, use other elements.
If you like to add and delete large objects frequently, use list
The object to be saved is not large, and the structure and destructor are not complex. You can use vector instead.
List <pointer> is the best way to achieve the lowest performance. In this case, it is better to use vector <pointer> because the pointer is not constructed or destructed, and does not occupy a large amount of memory.
 
3. deque
Deque is a double-ended queue and stores the content in the heap. it is saved as follows: [heap 1]... [heap 2]... [heap 3]
Each heap stores several elements, and there is a pointer between the heap and the heap. It looks like a combination of list and vector.
It supports the [] operator, that is, instant access, allowing you to quickly add and delete elements or add and delete elements later, then we can have a relatively high random access speed, which is almost the same as the efficiency of vector. It supports operations at both ends:
Push_back, push_front, pop_back, pop_front, and so on, and the operation at both ends is similar to the list efficiency.
In the standard library, vector and deque provide almost the same interface. In terms of structure, they differ mainly in the differences between the two containers in the organizational memory, deque allocates memory by page or block. Each page contains a fixed number of elements. on the contrary, the vector allocates a continuous memory. The vector is only inserted in the tail segment of the sequence.

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.