C + + stl,list vector differences

Source: Internet
Author: User

Sequential containers:

Vector vectors:

is a linear sequential structure. Equivalent to an array, but its size can be unspecified and automatically expanded. It can be manipulated like an array, and because of its nature we can view vectors as dynamic arrays.
After creating a vector, it automatically allocates a contiguous memory space in memory for data storage, the initial space size can be specified beforehand or by the vector by default, this size is the return value of the capacity () function. The vector allocates a chunk of memory when the stored data exceeds the allocated space, but the allocation is time-consuming and it does this when reallocating space:

First, the vector will apply for a larger chunk of memory;

Then, copy the original data into the new memory block;

Second, destroy the object in the original memory block (call the destructor of the object);

Finally, the original memory space is freed.

If the amount of data the vector is holding is large, such an operation will certainly result in poor performance (which is why the vector is designed to be a more easily copied value type). So vector is not under what circumstances performance is good, only in advance to know its size, the performance of the vector is optimal.

Features of Vector:
(1) specifies a block of contiguous storage as an array, but the space can be dynamically extended. That is, it can operate like an array, and it can be manipulated dynamically. Usually manifested in push_back () Pop_back ().
(2) Random access, it is accessed like an array, that is, support for the [] operator and vector.at ()
(3) Save space, because it is continuous storage, in the area of data storage is not wasted, but to make it clear that a little vector is not full, in most cases is not stored in the area is actually wasted.

(4) In the internal insert, delete operation is very inefficient, such operations are basically forbidden. Vectors are designed to append and delete operations on the backend only because the implementation of the vector is based on the principle of the sequential table.
(5) Push and pop can only be carried at the end of the vector, not on the head of the vector.
(6) When the dynamically added data exceeds the size assigned by the vector, the memory is redistributed, copied and released, which consumes performance. Therefore, to achieve the optimal performance of vectors, it is best to specify the space size when the vector is created.

Vector creates two-dimensional arrays with access:

Vector<vector<double>> vec2d (m,vector<double> (6));

Visit: vec2d[i][j] can!

Doubly linked list List

is a linear linked list structure whose data consists of several nodes, each of which consists of a block of information (that is, the actual data stored), a precursor pointer, and a back-drive pointer. It does not need to allocate the specified memory size and can scale arbitrarily, because it is stored in a non-contiguous memory space and is linked by a pointer to an ordered element.

Because of its structural reasons, the list random retrieval performance is very bad, because it does not directly find the address of the element as a vector, but to find the order from the beginning to the other, so that the more the target element, its retrieval time is longer. The retrieval time is proportional to the position of the target element.

Although the speed of random retrieval is not fast enough, it can be quickly inserted and deleted at any node. Because each node of the list holds its position in the linked list, inserting or deleting an element affects only a maximum of three elements, unlike vectors, which have no effect on the storage address of all elements after the operation point.

Features of list:
(1) Do not use continuous memory space so that can be arbitrarily dynamic operation;
(2) can be quickly inserted or deleted anywhere inside, of course, you can also push and pop at both ends.
(3) No internal random access is allowed, i.e. [] operator and vector.at () are not supported;
(4) Use more memory relative to Verctor.

Double-ended Queue deque
is an optimized basic sequence container for adding and deleting elements on both ends of a sequence. It allows for faster random access, but it does not keep all objects in a contiguous block of memory like a vector, but instead uses multiple contiguous blocks of storage and keeps track of those blocks and their order in a mapping structure. Adding or removing elements to deque is a small cost. It does not need to reallocate space, so adding elements to the end is more efficient than vector.

In fact, Deque is a combination of the pros and cons of vectors and lists, which is a container between the two.

Features of Deque:
(1) Convenient random access, that is, support [] operator and vector.at (), but the performance of the vector is not good;
(2) can be inserted and deleted internally, but the performance is not the list;
(3) can be push, pop at both ends;

Comparison of the three

Describes the features of the vector, list, and deque in the memory structure:

A vector is a contiguous block of memory, and Deque is a contiguous block of memory, and the list is stored separately for all data elements, which can be any two elements without a continuous.

The query performance of the vector is the best, and it is good to add data at the end, unless it re-applies to the memory segment and is suitable for efficient random storage.

A list is a linked list, and any element can be discontinuous, but it has two pointers to the previous and next elements. So it is the best performance for inserting and deleting elements, and the query performance is very poor; it is suitable for a large number of insert and delete operations without concern for random access requirements.

Deque is in between, and it takes into account the advantages of arrays and linked lists, which are the union of a chunked list and multiple arrays. So it has a good query performance by the list, has been good for the insertion of vectors, delete performance. Deque is the best choice if you need to immediately access and care about both data insertions and deletions.

C + + stl,list vector differences

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.