The difference of vector, list and deque of STL

Source: Internet
Author: User

1VectorThe vector corresponds to an array
Allocates a contiguous memory space in memory for storage. Supports storage that does not specify a vector size. STL internal implementation, first allocate a very large memory space to store, that is, the size of the Capacituy () function return, when more than this allocated space to redistribute a piece of memory storage, this gives a vector can not specify a vector that is the size of a contiguous memory. Typically, this default memory allocation completes most of the storage.
Advantages: (1) No contiguous storage of an array of memory size is specified, that is, it can be manipulated like an array, but the array can be
For dynamic operation. Usually embodied in push_back () Pop_back ()
(2) Convenience of random access, that is, support [] operator and vector.at ()
(3) Space saving.
Disadvantages: (1) The internal insertion delete operation is inefficient.
(2) Push and pop can only be done at the end of the vector and cannot be pushed and pop on the Vector's head.
(3) When the dynamically added data exceeds the vector default allocation size, the overall redistribution, copying and interpretation
Put

2List
Two-way linked list
Each node includes a info Quick info, a precursor pointer pre, and a rear-drive pointer post. You can add and remove operations without allocating the required memory size. is stored in a non contiguous memory space.
Advantages: (1) Do not use continuous memory to complete dynamic operation.
(2) Easy to insert and delete within the internal operation
(3) can be push at both ends, pop
Disadvantages: (1) cannot carry on the internal random access, namely does not support [] the operator and the vector.at ()
(2) compared with the verctor to occupy more memory

3deque
Double-end queue with two-terminal queues
The deque incorporates vectors and lists on a functional list.
Advantages: (1) Random access convenience, that is, support [] operator and vector.at ()
(2) Easy to insert and delete within the internal operation
(3) can be push at both ends, pop
Disadvantages: (1) occupy more memory

Use the difference:

1 If you need efficient and immediate access without caring about the efficiency of insertions and deletions, use vector
2 If you need a lot of insertions and deletions without concern for immediate access, you should use the list
3 If you need immediate access and care about inserting and deleting data at both ends, you should use the Deque


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.