Underlying implementation of vector, list, deque, set, and map in C ++

Source: Internet
Author: User
Difference between STL containers: vector list deque set map-underlying implementation

Difference between STL containers: vector list deque set Map)

In STL, the basic containers include vector, list, deque, set, and map.

Set and map are unordered storage elements. They can only be accessed through the interfaces provided by it.

Set: Set, used to determine whether an element is in a group and is rarely used.
Map: Ing,Equivalent to a dictionaryTo map a value to another value. If you want to create a dictionary, use it.
The underlying layer uses a tree structure, most of which are implemented using a balanced binary tree. It is a constant time to find a value, and the traversal results are good, but each time you insert a value, the underlying balance binary tree will be re-formed, and the efficiency will be affected.

Vector, list, And deque are ordered containers.
1.Vector
A vector is a dynamic array. It also allocates memory in the heap. elements are stored continuously with reserved memory,If the size is reduced, the memory will not be released. If the new value is greater than the current size, the memory will be allocated.

It has a continuous memory space and the starting address remains unchanged. Therefore, it supports instant access, that is, the [] operator,However, because its memory space is continuous, insertion and deletion in the middle will cause the copy of memory blocks.In addition,When the memory space after the array is insufficient, you need to apply for a memory that is large enough to copy the memory. These greatly affect the efficiency of vector.

The last element is the fastest to operate (the fastest to add and delete later). In this case, you do not need to move the memory. You only need to retain the memory when it is insufficient.


You need to move the memory to add and delete elements in the middle and the beginning,If your element is a structure or class, the structure and destructor will be performed while moving, so the performance is not high (it is best to put the pointer of the structure or class into the vector, instead of the structure or class itself, this can avoid the structure and structure when moving ).
In terms of access, the access to any element is O (1), that is, constant, so vector is often used to save the content that requires frequent random access, you do not need to add or delete intermediate elements frequently.

In comparison, we can see that the attributes of the vector are similar to those of the string. You can also use capacity to view the memory currently retained and use swap to reduce its memory usage.

Capacity () returns the number of elements that a vector can hold (without re-allocating memory) test push_back 1000 data capacity returns 16384

Summary
Please use vector for frequent Random Access

2.List
List isTwo-way linked list,Elements are also stored in the heap,Each element is put in a piece of memory, and its memory space can be discontinuous. It accesses data through pointers. This feature makes itRandom Access is very inefficient.Therefore, it does not provide overloading of the [] operator. However, due to the characteristics of the linked list, it supports deletion and insertion anywhere with good efficiency.

ListNo space reserved habitsTherefore, each allocated element is allocated from the memory, and each deleted element releases the memory occupied by it.

The performance of adding and deleting elements to a list is high, and the memory does not need to be moved. Of course, every element does not need to be constructed or parsed. Therefore, it is often used as a random operation container.
However, when accessing the elements in the list, the first and last access is the fastest.
O (n) is used to access other elements, so it is better to use other elements if frequent random access is required.

Summary
If you like to add and delete large objects frequently, use list
The object to be saved is not large, and the structure and destructor are not complex. You can use vector instead.
List <pointer> is the best way to achieve the lowest performance. In this case, it is better to use vector <pointer> because the pointer is not constructed or destructed, and does not occupy a large amount of memory.

3.Deque
Deque isDual-end queue(Double-ended Queue), also inContents stored in the heapIt is saved as follows:
[Heap 1]
...
[Heap 2]
...
[Heap 3]
Each heap stores several elements, and there is a pointer between the heap and the heap. It looks like a combination of list and vector.

It supports the [] operator, that is, instant access, allowing you to quickly add and delete elements or add and delete elements later, then there can be a relatively high random access speed, which is almost the same as the efficiency of vector. It supports operations at both ends: push_back, push_front, pop_back, pop_front, etc, in addition, the operation efficiency on both ends is similar to that on list.
In the standard library, vector and deque provide almost the same interface. In terms of structure, they differ mainly in the differences between the two containers in the organizational memory, deque allocates memory by page or block. Each page contains a fixed number of elements. on the contrary, the vector allocates a continuous memory. The vector is only effective when an element is inserted at the end of the sequence, the deque paging organization method provides insert and erase operations with constant time even at the front end of the container, and is more efficient than vector in terms of volume growth.

Summary:
Vector allows you to quickly add and delete elements at the end and quickly access any element.
List allows you to quickly add and delete elements in all places, but you can only quickly access the first and last elements.
Deque is as fast as adding elements at the beginning and end, and provides random access methods. Like a vector, deque uses [] to access any element, but random access speed is not as fast as that of vector, because it needs to handle heap redirection internally
Deque also has reserved space. in addition, because deque does not require continuous space, the elements that can be stored are larger than those of vector. in addition, when adding elements to the front and back, you do not need to move the elements of other blocks, so the performance is also high.

Therefore, in actual use, how to select one of the three containers should be determined according to your needs, generally follow the following
Principles:
1. If you need efficient instant access without worrying about the efficiency of insertion and deletion, use the Vector
2. If you need to insert and delete a large number of objects without having to access them immediately, you should use list
3. If you need immediate access and care about data insertion and deletion at both ends, use deque.

Source http://hi.baidu.com/zhouhq_926/blog/item/b1ee859762cc426754fb9665.html

Related Article

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.