C ++ container (4)

Source: Internet
Author: User

Vector, deque

And list


Ordered container:

Vector

Vector

:


Is a linear sequence structure. It is equivalent to an array, but its size can be unspecified and automatically expanded. It can be operated like an array. Due to its features, we can completely
As a dynamic array.

Create a vector
It will automatically allocate a continuous memory space in the memory for data storage. The initial space size can be specified in advance or by the vector
It is specified by default. The size is capacity.
() Return Value of the function. When the stored data exceeds the allocated space, the vector
A memory block will be re-allocated, but such allocation is very time-consuming. When the space is re-allocated, it will do the following:

First, Vector
A larger memory block will be applied;

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

Second, destroy the objects in the original memory block (call the object's destructor );

Finally, release the original memory space.

If the Vector
When the stored data volume is large, such operations will inevitably lead to poor performance (this is also a vector
The reason is designed to be a value type that is easier to copy ). So Vector
It is not in any situation that the performance is good, but only when the size of the vector is known in advance.
The performance is optimal.

Vector
Features:
(1)
Specify continuous storage as an array, but the space can be dynamically expanded. That is, it can operate like an array and can perform dynamic operations. Usually reflected in push_back () pop_back ()
.
(2)
Random Access is convenient. It is accessed like an array, that is, it supports []
Operator and vector. ()
(3)
It saves space because it is continuously stored and is not wasted in the area where data is stored, but make it clear that vector
In most cases, it is not full storage, but it is actually a waste in areas that are not stored.

(4)
The efficiency of internal insert and delete operations is very low. Such operations are basically forbidden. Vector
It is designed to perform append and delete operations only on the backend. The reason is that the vector
The internal implementation is based on the sequence table principle.
(5)
Only in the vector
Finally push
And pop
, Cannot be in the vector
To push
And pop
.
(6)
When the dynamically added data exceeds the Vector
By default, memory needs to be re-allocated, copied, and released in an hour, which consumes a lot of performance.

So Vector
To achieve optimal performance, it is best to create a vector
Specify the space size.

Bidirectional linked list

Is a Linear Linked List structure. Its data consists of several nodes, each of which includes an Information block (actually stored data), a forward pointer, and a rear drive pointer. It does not need to allocate a specified memory size and can be scaled at will, because it is stored in non-contiguous memory space and linked by pointers to ordered elements.

Because of its structure, list
The random search performance is very poor because it is not like a vector
In this way, the address of the element is directly found, but it needs to be searched one by one from the beginning. In this way, the closer the target element is to the back, the longer its retrieval time. The Retrieval time is proportional to the location of the target element.

Although the random search speed is not fast enough, it can be quickly inserted or deleted on any node. Because list
Each node stores its position in the linked list. inserting or deleting an element only affects up to three elements, unlike the Vector
It will affect the storage addresses of all elements after the operation point, which is a vector
Incomparable.

List
Features:
(1)
Without continuous memory space, you can perform dynamic operations at will;
(2)
It can be quickly inserted or deleted anywhere inside, or pushed at both ends
And pop
.
(3)
Internal Random Access is not allowed, that is, []
Operator and vector. ()
;
(4)
Compared with verctor
Use more memory.

Deque




It is an optimized basic sequence container that adds and deletes elements at both ends of a sequence. It allows fast random access, but it is not like a vector
All objects are stored in a continuous memory block. Instead, multiple consecutive storage blocks are used, and the tracing of these blocks and their sequence is saved in a ing structure. To deque
The overhead of adding or deleting elements at both ends is small. It does not need to re-allocate space, so adding elements to the end of the Vector
More effective.

Actually, deque
Is for Vector
And list
The combination of advantages and disadvantages is a container between the two.

Deque
Features:
(1)
Convenient Random Access: []
Operator and vector. ()
But the performance is not Vector
Good;
(2)
Insert and delete operations can be performed internally, but the performance is not as good as list
;
(3)
Push can be performed on both ends
Pop
;

Comparison of the three

Describes the Vector
, List
, Deque
Features in the memory structure:

Vector
Is a continuous memory block, and deque
Is multiple consecutive memory blocks, list
All data elements are stored separately. It can be that any two elements are not consecutive.

Vector
The query performance is the best, and it is also good to add data at the end, unless it applies for a memory segment again; suitable for efficient Random storage.

List
It is a linked list. Any element can be discontinuous, but it has two pointers pointing to the previous element and the next element. Therefore, it provides the best performance for inserting and deleting elements, while the query performance is very poor.
A large number of insert and delete operations do not care about random access needs.

Deque
Is between the two, it takes into account the advantages of arrays and linked lists, it is a block linked list and the combination of multiple arrays. So it has a list
Good query performance, with Vector
Good insertion and deletion performance.
Deque
Is the best choice.
From: blog.csdn.net/acossoft

 

 

 

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.