The difference between vector and list and deque and their respective advantages and disadvantages

Source: Internet
Author: User
Vectors and lists and deque differ in their respective pros and cons: vector: Advantages: Random access, that is, [] operations and vector.at () dynamic operations, push_back () and Pop_back () automatically adjust memory, save space Disadvantage: The efficiency of insertion and deletion can only be inserted and deleted at the end, and the size of the insertion and deletion time in the head is proportional to the size of the container. When dynamically added data exceeds the default memory size, the overall redistribution, copying, and release are performed. List: Advantages: Memory discontinuous dynamic operation, insertion and deletion efficiency can be pop,push at both ends of the disadvantage: can not be randomly accessed relative to the vector memory more deque: Advantages: Random access, that is, [] operation and deque.at () insertion and deletion of the convenience can be in the two-terminal pop, Push disadvantage: Consumes more memory.
To sum up the three is not as follows: The same point: three can achieve resize () to resize the container. Different points: 1 vector can achieve immediate access, that is, [] operations, and list can not, deque is the combination of both, but also to achieve index operation [], but the efficiency is not high vector. 2 the vector is suitable for the operation of the deletion element at the end of the file Pop_back () and insert Operation Push_back (), the efficiency is very low in the middle. The list can be inserted and deleted at any point in the container. Reason: Vector's element address is contiguous, if insert and delete in the middle, may cause the original memory address is not enough to store the current element, need to reassign memory address, this need to copy all the original elements to new memory, release the old memory address and other operations, the operation is costly. The list element memory address is not continuous, with the pointer operation, which itself is a two-way linked list, its high efficiency reflected in the insertion, deletion and sorting, such as moving a large number of elements of the operation. Criteria for choosing a container type: 1 If you need to randomly access a container, the vector is 2 better than the list if you frequently need to insert or delete container elements, the list is 3 better than the vector if you want random access, but also care about the insertion and deletion of data at both ends, select 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.