Analysis of C + + VECTOR,LIST,DEQUEUE,STACK storage structure

Source: Internet
Author: User

Tag:vector   list   dequeue   stack    storage structure     

Vector applies: The number of objects changes less, simple objects, random access elements frequently.
List applies: the number of objects varies greatly, objects are complex, insertions and deletions are frequent.
The biggest difference is that the list is bidirectional, and the vector is one-way.
Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following principles:
1, if you need to efficiently immediately access, but do not care about the efficiency of insertion and deletion, using vector;7
2, if you need a large number of insertions and deletions, and do not care about immediately access, you should use list;
3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.


Vectors represent a contiguous area of memory where each element is stored sequentially in this memory, and the random access to vectors is highly efficient, but the insertion and deletion of non-end elements is very inefficient. Inserting or deleting an element in Vecotor requires that an existing element be copied and moved. If the objects stored in the vector are large, or the constructors are complex, it is expensive to copy the existing elements because the Copy object calls the copy constructor. For simple small objects, vectors are more efficient than list. Vector expands the capacity by twice times each time it expands, which is very efficient for small objects.

The list represents a non-contiguous area of memory and is linked by a pair of pointers to the end-to-end elements, with high efficiency and low random access efficiency. The objects in the list are stored in discrete, random access to an element that needs to traverse the list. Inserting elements into the list, especially at the end of the insert, is highly efficient and requires only changing the pointer of the element.


Deque
Also represents a contiguous area of memory, but unlike a vector it supports the efficient insertion and deletion of elements in its header, implemented by a two-level array structure, one level represents the actual container, and the second level points to the first and the end of the container.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of C + + VECTOR,LIST,DEQUEUE,STACK storage structure

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.