Differences between vector and list in c ++

Source: Internet
Author: User

In the c ++ standard library, both the vector and list containers can be used to store a group of data of the same type. What's different from arrays is that they support dynamic growth. However, there are several differences: (1) vector is a sequence table, indicating a continuous memory, and elements are stored in sequence; list is a two-way connection table, which is not necessarily continuous in memory. (2) When the value memory is not enough, the vector will apply for a large enough continuous memory to copy the original data to the new memory. The list does not need to consider memory continuity, therefore, the new overhead is smaller than that of vector. (3) A list can only access elements through pointers. The efficiency of random element access is very low. It is more appropriate to use a vector when frequent random element access is required. (4) When you insert or delete an element to a vector, You need to copy and move all the elements on the right of the element to be inserted. Therefore, it is more appropriate to use list when you insert or delete an element frequently.

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.