The differences between vector map list and queue are analyzed in detail

  • 2020-04-02 01:39:32
  • OfStack

1. The vector   Fast access to random elements, fast insertion at the end of the element, but slow insertion in the middle of the sequence, slow deletion of the element, and a performance overhead of redistributing more space and then copying if there is not enough space allocated at the beginning.

2. A deque (continuous dice, dice with a linked list is linked together, in fact within a map of a pointer, because you know that type, so you can use [], just fast without vector) rapid access to random elements, fast in the beginning and the end of the insert element, random insertion, deletion slower elements, space redistribution is quicker than vector, to allocate space, do not need to copy the original elements. The sorting operation of deque can first copy deque to vector and then copy back to deque after sorting.

3. The list     (each element is linked with a linked list) access to random elements is not as fast as vector, random inserted elements are faster than vector, the allocation of space for each element, so there is not enough space, redistribution of the situation

4. The set The internal element is unique and is stored in a balanced tree structure, so it is sorted when traversing and found quickly.

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

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

7. The queue Is a restricted deque, the internal container is generally easier to use list. Fifo, no traversal allowed.

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

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

3. If we need to do more than insert and delete elements at both ends of the container, list is obviously better than vector  

Vector is better than deque unless we need to insert and delete elements at the top of the container.

5. Select deque if you only insert data elements at the easy head and tail.

6. If you only need to insert an element in the middle of the container when reading the input, and then need to access the element randomly, you can consider reading the element into a List container at the time of input, then relearning the container to make it suitable for sequential access, and then copying the sorted List container into a vector container


Related articles: