Http://blog.csdn.net/xfortius/article/details/7760490
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 regard vector as a dynamic array.
After a vector is created, it automatically allocates a continuous memory space in the memory for data storage. The initial space size can be specified in advance or by default, this size is the return value of the capacity () function. When the stored data exceeds the allocated space, the vector will re-allocate a block of memory, but such allocation is very time-consuming. When the space is re-allocated, it will do the following:
First, vector will apply for a larger memory block;
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 a vector stores a large amount of data, such an operation will certainly lead to poor performance (this is also the reason why the vector is designed to be a value type that is easier to copy ). Therefore, the performance of a vector is optimal only when its size is known in advance.
Features of vector:
(1) specify a 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, the [] Operator and vector. At () are supported ()
(3) It saves space because it is continuously stored and is not wasted in the area where data is stored, but it should be clear that the vector is not fully stored in most cases, it is actually a waste in areas not stored.
(4) The efficiency of internal insert and delete operations is very low. Such operations are basically prohibited. The vector is designed to be appended and deleted only at the backend, because the implementation in the vector is based on the principle of the sequence table.
(5) Only push and pop can be performed at the end of the vector, and push and pop cannot be performed at the header of the vector.
(6) When the dynamically added data exceeds the size allocated by the vector by default, the memory needs to be re-allocated, copied, and released. This operation consumes a lot of performance. To achieve optimal performance, it is best to specify the space size when creating a vector.
Vector creates two-dimensional arrays and accesses:
Vector <vector <double> vec2d (M, vector <double> (6 ));
Access: vec2d [I] [J!
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, the list random search performance is very poor, because it does not directly find the element address as a vector, but needs to look up from the beginning one by one, so that the target element is closer to the back, the longer it takes to search. 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 each node in the list stores its position in the linked list, inserting or deleting an element only affects up to three elements, unlike vector, which affects the storage addresses of all elements after the operation point, it is incomparable to vector.
Features of list:
(1) dynamic operations can be performed without the use of continuous memory space;
(2) It can be quickly inserted or deleted anywhere inside the system. Of course, it can also be pushed or pop at both ends.
(3) Internal Random Access is not allowed, that is, the [] Operator and vector. At () are not supported ();
(4) It occupies more memory than verctor.
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 unlike vector, it stores all objects in a continuous memory block, but uses multiple consecutive storage blocks, in addition, the tracing of these blocks and their sequence is saved in a ing structure. The overhead of adding or deleting elements to or from both ends of deque is very small. It does not need to re-allocate space, so adding elements to the end is more effective than vector.
In fact, deque is a combination of the advantages and disadvantages of vector and list. It is a container between the two.
Deque features:
(1) the random access is convenient, that is, the [] Operator and vector. At () are supported, but the performance is not good at vector;
(2) you can insert or delete data internally, but the performance is not as good as list;
(3) Push and pop can be performed on both ends;
Comparison of the three
Describes the features of the memory structure of vector, list, And deque:
Vector is a continuous memory block, while deque is multiple consecutive memory blocks. list is the storage of all data elements separately. It can be that any two elements are not consecutive.
The query performance of vector 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 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 has the best performance for inserting and deleting elements, while the query performance is very poor. It is suitable for a large number of insert and delete operations without worrying about random access requirements.
Deque is between the two. It takes into account the advantages of arrays and linked lists. It is a combination of chunk linked lists and multiple arrays. Therefore, it has the query performance of list, and the insertion and deletion performance of vector. If you need to immediately access and care about data insertion and deletion at both ends, deque is the best choice.