[STL] differences between the vector list deque object in the standard library

Source: Internet
Author: User

1 vector
Vector is equivalent to an array
Allocate a continuous memory space in the memory for storage. Storage with no vector size specified is supported. During internal implementation of STL, a very large memory space is allocated for storage, that is, the size returned by the capacity () function, when the size of the allocated space is exceeded, a memory storage will be re-allocated. This gives us the feeling that the vector can be a continuous memory size without being specified. Generally, the default memory allocation can complete storage in most cases.
Advantages:
(1) Specify the continuous storage of an array of memory size, which can be operated like an array, but can be dynamically operated on this array. Usually reflected in push_back () pop_back ()
(2) convenient random access, that is, support for the [] Operator and vector. ()
(3) space saving.
Disadvantages:
(1) It is inefficient to insert or delete data internally.
(2) Push and pop can only be performed at the end of the vector, and push and pop cannot be performed at the header of the vector.
(3) When the dynamically added data exceeds the size allocated by the vector by default, the whole data needs to be re-allocated, copied, and released.
2 list
Two-way linked list: each node includes a fast info, a pre-pointer, and a post-drive pointer. You can easily add or delete memory without allocating the required memory size. Uses non-contiguous memory space for storage.
Advantages:
(1) do not use continuous memory for dynamic operations.
(2) Easy internal insertion and deletion operations
(3) Push and pop can be performed on both ends
Disadvantages:
(1) Internal Random Access is not allowed, that is, the [] Operator and vector. At () are not supported ()
(2) memory usage compared with verctor
3 deque
Double-end queue
Deque combines the vector and list functions.
Advantages:
(1) convenient random access, that is, support for the [] Operator and vector. ()
(2) Easy internal insertion and deletion operations
(3) Push and pop can be performed on both ends
Disadvantages:
(1) high memory usage

Usage differences:
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 to access data immediately and care about data insertion and deletion at both ends, use deque

Tip: http://blog.csdn.net/sunboy_2050/article/details/6121890 #

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.