The difference of vector,map,list,queue in detail _c language

Source: Internet
Author: User

1. Vector (continuous space storage, you can use the [] operator to quickly access random elements, quickly insert elements at the end, but in the middle of the sequence of insertion, delete elements slow, and if the initial allocation of space is not enough, there is a redistribution of larger space, and then copy the performance cost .

2. Deque (continuous of small pieces, the small pieces are connected by a linked list, and in fact there is a pointer to a map, because you know the type, so you can use [], just not speed vector fast access to random elements, fast at the beginning and end of the insertion of elements, random inserts, delete elements to slow , the redistribution of space is faster than the vector, and once the space is redistributed, the original elements do not need to be copied. For the Deque sort operation, the deque can be copied to the vector first, sorted after being copied back to Deque.

3. List (linked to each element) access to random elements is not as fast as vector, random insertion element faster than vector, allocate space for each element, so there is no room for redistribution

4. Set internal element unique, with a balanced tree structure to store, so the time to traverse the sort, and find a faster oh.

5. The combination of map one-to-one mappings, key cannot be repeated.

6. The stack adapter must be used in conjunction with other containers, and the default internal container in the STL is deque. Advanced after out, only one exit, not allowed to traverse.

7. Queue is a restricted deque, and internal containers generally use the list simpler. Advanced first out, not allowed to traverse.

Below are some guidelines for selecting a sequential container type  
1. If we need random access to a container, the vector is much better than the list.

2. If we know the number of elements to store, vector is a better choice than list.  

3. If we need more than just inserting and deleting elements at both ends of the container, then list is obviously better than vector  

4. Unless we need to insert and delete elements in the first container, the vector is better than the deque.

5. If you only insert data elements in the easy header and tail, select Deque.

6. If you only need to insert an element in the middle of the container while reading the input, then you need to randomly access the elements, you can consider entering the elements into a list container, and then pat the container to fit in sequential access, and then copy the sorted list container into a vector container

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.