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