Container to be selected

Source: Internet
Author: User

The following lists some rules for selecting the container type:

(1) IfProgramIf random element access is required, the vector or deque container should be used.

(2) If the program must insert or delete elements in the middle of the container, the list container should be used.

(3) If the program is not in the middle of the container, but inserts or deletes elements in the header or tail of the container, deque container should be used.

(4) If you only need to insert an element in the middle of the container when reading the input and then randomly access the element, you can consider reading the element into a list container during the input, then the containers are re-sorted to make them suitable for sequential access, and then the sorted list containers are copied to a vector container.

 

What should I do if the program needs random access and must insert or delete elements in the middle of the container?

In this case, the selected container depends on the relative cost of the following two operations: the cost of Random Access to the elements of the list container, and the cost of copying elements when inserting and deleting elements in the vector or deque container. Generally, the dominant operation in an application (the program uses more access operations, insert operations, and delete operations) will determine the type of container to be selected. Decide which container to use may require profiling of the performance of various operations required by various container types to complete the application. If you cannot determine which container should be used by an application, writeCodeTo avoid random access to elements. In this way, you can easily modify the program from using the vector container to using the list container when necessary.

 

For most applications, using a vector container is the best. The reason is that the implementers of the standard library use the memory allocation policy to store elements continuously at the minimum cost. The convenience of access elements makes up for the storage cost.

To enable the vector container to implement fast memory allocation, the actual allocated capacity is more than the current space required. The vector container reserves these additional storage areas for storing newly added elements. Therefore, you do not have to re-allocate containers for each new element. The exact number of allocated extra memory capacity varies depending on the library implementation. This allocation policy brings significant efficiency compared to the need to re-allocate a container every time a new element is added. In fact, its performance is very good, so in practical applications, compared with the list and deque containers, the growth efficiency of vector is usually higher.

Source: C ++ Primer

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.