STL Containers

What are STL Containers?

STL containers are predefined classes or templates in C++ that offer dynamic memory allocation and provide various functionalities to store and manipulate collections of objects. These container encapsulate common data structures such as arrays, linked lists, stacks, queues, and associative arrays (maps and sets), abstracting away low-level details and providing a consistent interface for interaction.

+------------------+
| Container Types  |
+------------------+
          |
          V
    +-----------------+
    |   Sequence      |
    |   Containers    |
    +-----------------+
          |
   +------+------+
   |             |
   V             V
+--------+    +---------+
| Vector |    |   List  |
+--------+    +---------+
   |             |
   V             V
+--------+    +---------+
| Deque  |    | Forward |
+--------+    |   List  |
              +---------+
                  |
                  V
              +--------+
              |   Array|
              +--------+
    
+------------------+
|   Associative    |
|   Containers      |
+------------------+
          |
   +------+-------+
   |              |
   V              V
+--------+     +--------+
|  Set   |     |   Map  |
+--------+     +--------+
   |              |
   V              V
+--------+     +-----------+
|  Multi |     |  MultiMap  |
|   Set  |     |            |
+--------+     +-----------+

+------------------+
|   Unordered      |
|   Containers      |
+------------------+
          |
   +------+-------+
   |              |
   V              V
+--------+     +-----------+
| U_Set  |     | U_MultiSet|
+--------+     +-----------+
   |              |
   V              V
+--------+     +-----------+
| U_Map  |     | U_MultiMap|
+--------+     +-----------+

Different Types of Containers

1 Sequence Containers:

Sequence containers maintain the order of elements as they are inserted. They offer direct access to individual elements through iterators.

2 Associative Containers:

Associative containers organize elements based on keys rather than their order of insertion. They are implemented using self-balancing binary search trees or hash tables. They provide efficient lookup, insertion, and deletion based on the keys and include sets, multisets, maps, and multimaps.

3 Container Adapters:

Container adapters are special types of containers that are built on top of sequence containers to provide restricted functionality. The primary examples are stack, queue, and priority queue.

4 Unordered Associative Containers (Hash-based):

Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched.

These containers provide faster average-case time complexities for insertion, deletion, and search operations compared to their tree-based counterparts. They are beneficial when the order of elements is not crucial, and fast access to elements is a priority.  Hash-based containers are particularly useful in scenarios where performance matters more than maintaining a specific order.