The difference between a vector and a list

Source: Internet
Author: User

1

The difference between a vector and a list
Vector for the stored objectallocate a contiguous address space, so the random access to the elements in the vector is highly efficient. 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 objects in the list areof discrete storage, random access to an element requires traversing 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.
In Summary:
Vector applies: Small number of objects change, 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
Principle of:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the 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.
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, the second level points to the first and the end of the container

List represents a non-contiguous area of memory and is linked in two directions through a pair of pointers to the end-to-end elements, with high insertion efficiency and low random access efficiency



2

STL provides three of the most basic containers: Vector,list,deque.

Vector andbuilt-in ArraySimilarly, it has a contiguous memory space and the starting address is the same, so
It can be very well supported immediately, that is, the [] operator, but because its memory space is continuous, so in the middle
Inserting and deleting causes a copy of the block of memory, and when there is not enough memory space behind the array, you need to re-request a chunk of memory that is large enough to make a copy of the memory. These have greatly affected the efficiency of vectors.

The list is the data structure.doubly linked list(according to the SGI STL source code), so its memory space can be discontinuous
Access to data through pointers, which makes it very inefficient to access it immediately, so it
No overloads are provided for the [] operator. But due to the characteristics of the list, it can be very efficient to support the deletion of any place
and insert.

Deque is adouble-ended Queue, its implementation is not very clear, but it is known to have the following two features:
Itsupports the [] operator, which is the support for immediately accessing, and the efficiency of the vector is similar, it supports at both ends of the
Operation: Push_back,push_front,pop_back,pop_front, and the efficiency of the list on both sides of the operation
also similar.

Therefore, in the actual use, how to choose which of the three containers, should be based on your needs, should generally follow the following
Principle of:
1. If you need efficient immediate access, without the efficiency of insert and delete, use vector
2, if you need a large number of insertions and deletions, and do not care about the immediate access, you should use the list
3, if you need to immediately access, and care about the end of data insertion and deletion, you should use Deque.

The difference between a vector and a list

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.