Container Classes and Iterators in C++ STL
The following notes describe Container Classes and Iterators as fundamental components of the C++ Standard
Template Library (STL) used for managing collections of data.
Container Classes
A container class is designed to store multiple values in a single object and provides functions to manage that data,
such as inserting, removing, and accessing elements. Key features include automatic memory management, type
safety via templates, and efficiency through optimized internal algorithms.
Sequence Containers
These store elements in a linear order.
Vector: Stores elements in continuous memory, allowing for fast access.
List: A doubly linked list that allows fast insertion or deletion at any position.
Deque: A double-ended queue that allows insertion at both the front and the back.
Associative Containers
These store elements in sorted order or as key-value pairs.
Set/Multiset: Sets store unique elements, while multisets allow duplicate values.
Map/Multimap: Store data as key-value pairs in sorted order. Maps require unique keys, while multimaps allow
duplicate keys.
Unordered Containers
These use hash tables for faster access. Because they use hash functions to jump directly to a required location, the
order of elements is not fixed. Examples include unordered_set and unordered_map.
Container Adaptors
These provide a limited interface based on existing containers to follow specific data structures.
Stack: Follows a Last-In, First-Out (LIFO) structure.
Queue: Follows a First-In, First-Out (FIFO) structure.
Priority Queue: Always returns the largest element first.
Iterators
An iterator is an object that acts like a pointer, allowing a programmer to move through and access elements within a
container without needing to understand its internal structure. This provides a uniform way to apply algorithms like
searching or sorting across different types of containers.
Common Iterator Operations
Operation Description
begin() Points to the first element
end() Points to the position just after the last element
++it Moves the iterator to the next element
*it Accesses the value at the current iterator position