Summary of Containers in C++ STL

The commonly used containers are summarized as follows:

  • vector : A dynamic array container that supports random access, tail insertion and deletion, and is suitable for scenarios that require fast random access and infrequent addition and deletion of elements.

  • list : A doubly linked list container that supports bidirectional iterators, high insertion and deletion operations, and is suitable for scenarios with frequent insertion and deletion operations.

  • deque : A double-ended queue container that supports random access, head-to-tail insertion and deletion, and is suitable for scenarios that require frequent insertion and deletion of elements at the head and tail.

  • stack : The stack container, implemented with deque or list at the bottom layer , only supports inserting and deleting elements at the top of the stack, and is suitable for scenarios that require last-in-first-out (LIFO).

  • queue : Queue container, implemented with deque or list at the bottom layer , only supports insertion at the end of the queue and deletion of elements at the head of the queue, suitable for scenarios that require first-in-first-out (FIFO).

  • priority_queue : Priority queue container, implemented with a heap at the bottom layer, supports quick search and deletion of elements on the top of the heap, and is suitable for scenarios where elements need to be processed according to a certain priority.

  • set : The collection container, implemented with red-black tree at the bottom layer, supports insertion, deletion and search of elements, and the elements are automatically sorted according to the key value, which is suitable for scenarios that need to be sorted according to the key value and cannot have duplicate elements.

  • map : Mapping container, implemented by red-black tree at the bottom layer, supports fast search, insertion and deletion of values ​​by key, and elements are automatically sorted by key value, suitable for scenarios that need to be sorted by key value and cannot have duplicate keys.

  • unordered_set : A collection container implemented by a hash table, which supports fast insertion, deletion, and search of elements, and does not guarantee the order of elements. It is suitable for scenarios that require fast search, insertion, and deletion of elements without order.

  • unordered_map : A mapping container implemented by a hash table, which supports fast lookup, insertion, and deletion of values ​​by key, and does not guarantee the order of elements. It is suitable for scenarios that require fast lookup, insertion, and deletion of key-value pairs and does not require order.

Guess you like

Origin blog.csdn.net/hu853712064/article/details/129767367