Vector list deque

Source: Internet
Author: User

STL provides three basic containers: vector, list, And deque.

Like the built-in array, vector has a continuous memory space and the starting address remains unchanged. Therefore, it supports instant access, that is, the [] operator, however, because its memory space is continuous, insertion and deletion in the middle will result in a copy of the memory block. In addition, when the memory space after the array is insufficient, you need to apply for a large enough memory and copy the memory. These greatly affect the efficiency of vector.

List is a two-way linked list in the data structure (based on the sgi stl source code). Therefore, its memory space can be discontinuous and data access can be performed through pointers, this feature makes access to it 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.

Deque is a double-ended queue. Its implementation is unclear, but it has the following two features:
It supports [] operators, that is, instant access, and is less efficient than vector. It supports operations at both ends: push_back, push_front, pop_back, pop_front, etc, in addition, the operation efficiency on both ends is similar to that on list.

Therefore, in actual use, how to select one of the three containers should be determined according to your needs, generally follow the following
Principles:
1. If you need efficient instant access without worrying about the efficiency of insertion and deletion, use the Vector
2. If you need to insert and delete a large number of objects without having to access them immediately, you should use list
3. If you need immediate access and care about data insertion and deletion at both ends, use deque.

 

 

Vector allocates a continuous address space for the stored objects. Therefore, it is highly efficient to randomly access elements in the vector. To insert or delete an element in the vecotor, you must copy and move the existing element. If the objects stored in the vector are large, or the constructor is complex, the overhead of copying existing elements is large, because the copy constructor must be called to copy objects. For simple small objects, vector is more efficient than list. Each time a vector expands its capacity, it doubles the capacity, which is highly efficient for small objects.

Objects in the list are stored discretely. to randomly access an element, You need to traverse the list. It is very efficient to insert elements into the list, especially at the beginning and end. You only need to change the element pointer.

To sum up:

Applicable to vector: the number of objects changes little, simple objects, and frequent Random Access to elements

Applicable to list: large changes in the number of objects, complex objects, and frequent insertion and Deletion

Site: http://w57w57w57.blog.163.com/blog/static/9607473520094751136967/

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.