[C++] The difference between vector, list and deque

vector encapsulates the array, has a continuous memory space, and the starting address remains unchanged. So vector can support random access very well, support [] operator overloading and vector.at().

But because its memory space is continuous, inserting and deleting in the middle of the vector will cause the copy and movement of the memory block, and the complexity is O(n). In addition, when the memory space of the array is not enough, it is necessary to reapply for a large enough memory and copy the memory, which reduces the efficiency of the vector.

But the insertion speed at the end of the vector is very fast, all vectors have push_back() and pop_back() operations.


The list encapsulates a doubly linked list, so its memory space can be discontinuous, so data can only be accessed through pointers. The random access of the list becomes very inefficient, it needs to traverse all the elements, and the search complexity is O(n).

But the list can easily support deletion and insertion at any position, as long as the pointer is modified. List takes up more memory than vector.


The deque double-ended queue, which combines vector and list in function, is convenient for random access, can be inserted and deleted conveniently inside, and can be pushed and poped at both ends. The disadvantage is that it takes up a lot of memory.


Guess you like

Origin blog.csdn.net/michellechouu/article/details/50705766
Recommended