The Wayback Machine - https://web.archive.org/web/20240718145154/https://www.geeksforgeeks.org/containers-cpp-stl/
Open In App

Containers in C++ STL (Standard Template Library)

Last Updated : 11 Jan, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows great flexibility in the types supported as elements. 

The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators (reference objects with similar properties to pointers). 

Sequence containers

Sequence containers implement data structures that can be accessed sequentially. 

  • array: Static contiguous array (class template)
  • vector: Dynamic contiguous array (class template)
  • deque: Double-ended queue (class template)
  • forward_list: Singly-linked list (class template)
  • list: Doubly-linked list (class template)

Associative containers

Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity). 

  • Set: Collection of unique keys, sorted by keys 
    (class template)
  • Map: Collection of key-value pairs, sorted by keys, keys are unique (class template).
  • multiset: Collection of keys, sorted by keys (class template)
  • multimap: Collection of key-value pairs, sorted by keys 
    (class template)

Unordered associative containers

Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) amortized, O(n) worst-case complexity). 

Container adapters

Container adapters provide a different interface for sequential containers. 

  • stack: Adapts a container to provide stack (LIFO data structure) (class template).
  • queue: Adapts a container to provide queue (FIFO data structure) (class template).
  • priority_queue: Adapts a container to provide priority queue (class template). 
     

Flowchart of Adaptive Containers and Unordered Containers

Flowchart of Sequence containers and ordered containers

More Useful Links 

To master C++ Standard Template Library (STL) in the most efficient and effective way, do check out this C++ STL Online Course by GeeksforGeeks. The course covers the basics of C++ and in-depth explanations to all C++ STL containers, iterators, etc along with video explanations of a few problems. Also, you’ll learn to use STL inbuilt classes and functions in order to implement some of the complex data structures and perform operations on them conveniently.


Previous Article
Next Article

Similar Reads

The C++ Standard Template Library (STL)
The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized. Working knowledge of template classes is a prerequ
5 min read
Sort in C++ Standard Template Library (STL)
Sorting is one of the most basic functions applied to data. It means arranging the data in a particular fashion, which can be increasing or decreasing. There is a builtin function in C++ STL by the name of sort(). This function internally uses IntroSort. In more details it is implemented using hybrid of QuickSort, HeapSort and InsertionSort.By defa
6 min read
Priority Queue in C++ Standard Template Library (STL)
A C++ priority queue is a type of container adapter, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue, and elements are in non-increasing or non-decreasing order (hence we can see that each element of the queue has a priority {fixed order}). In C++ STL, the top elemen
11 min read
Binary Search in C++ Standard Template Library (STL)
Binary search is a widely used searching algorithm that requires the array to be sorted before search is applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the element is found, or all the elements are exhausted.It works by comparing the middle item of the array with our target, if it match
3 min read
Multimap in C++ Standard Template Library (STL)
Multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. One important thing to note about multimap is that multimap keeps all the keys in sorted order always. These properties of multimap make it very much useful i
6 min read
Multiset in C++ Standard Template Library (STL)
Multisets are a type of associative containers similar to the set, with the exception that multiple elements can have the same values. Some Basic Functions associated with multiset: begin() - Returns an iterator to the first element in the multiset --> O(1)end() - Returns an iterator to the theoretical element that follows the last element in th
6 min read
Queue in C++ Standard Template Library (STL)
Queues are a type of container adaptors that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. Queues use an encapsulated object of deque or list (sequential container class) as its underlying container, providing a specific set of member functions to access its eleme
3 min read
Deque in C++ Standard Template Library (STL)
Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements. Unlike vectors, contiguous storage allocation may not be guaranteed. Double Ended Queues are basically an implementation of the data structure doub
4 min read
Pair in C++ Standard Template Library (STL)
Pair is used to combine together two values that may be of different data types. Pair provides a way to store two heterogeneous objects as a single unit. It is basically used if we want to store tuples. The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referen
9 min read
List in C++ Standard Template Library (STL)
Lists are sequence containers that allow non-contiguous memory allocation. As compared to the vector, the list has slow traversal, but once a position has been found, insertion and deletion are quick (constant time). Normally, when we say a List, we talk about a doubly linked list. For implementing a singly linked list, we use a forward_list. std::
6 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg