Modern C++
Programming
20. Containers, Iterators,
Ranges, and Algorithms
Federico Busato
2026-01-06
Table of Contents
1 Containers and Iterators
Semantic
2 Sequence Containers
std::array
std::vector
std::deque
std::list
std::forward_list
1/69
Table of Contents
3 Associative Containers
std::set
std::map
std::multiset
4 Container Adaptors
std::stack, std::queue, std::priority_queue
5 Implement a Custom Iterator
Implement a Simple Iterator
2/69
Table of Contents
6 Iterator Notes
7 Iterator Utility Methods
std::advance, std::next
std::prev, std::distance
Container Access Methods
Iterator Traits
3/69
Table of Contents
8 Algorithms Library
std::find_if, std::sort
std::accumulate, std::generate, std::remove_if
4/69
Table of Contents
9 C++20 Ranges
Key Concepts
Range View
Range Adaptor
Range Factory
Range Algorithms
Range Actions
5/69
Containers and
Iterators
Containers and Iterators
Container
A container is a class, a data structure, or an abstract data type, whose instances
are collections of other objects
Containers store objects following specific access rules
Iterator
An iterator is an object allowing to traverse a container
Iterators are a generalization of pointers
A pointer is the simplest iterator, and it supports all its operations
C++ Standard Template Library (STL) is strongly based on containers and
iterators
6/69
Reasons to use Standard Containers
STL containers eliminate redundancy, and save time avoiding writing your own
code (productivity)
STL containers are implemented correctly, and they do not need to spend time to
debug (reliability)
STL containers are well-implemented and fast
STL containers do not require external libraries
STL containers share common interfaces, making it simple to utilize different
containers without looking up member function definitions
STL containers are well-documented and easily understood by other developers,
improving the understandability and maintainability
STL containers are thread safe. Sharing objects across threads preserve the
consistency of the container
7/69
Container Properties
C++ Standard Template Library (STL) Containers have the following properties:
Default constructor
Destructor
Copy constructor and assignment (deep copy)
Iterator methods begin() , end()
Support std::swap
Content-based and order equality ( ==, != )
Lexicographic order comparison ( >, >=, <, <= )
size()
, empty() , and max_size() methods
except for std::forward_list
8/69
Iterator Concept
STL containers provide the following methods to get iterator objects:
begin() returns an iterator pointing to the first element
end() returns an iterator pointing to the end of the container (i.e. the element after the
last element)
There are different categories of iterators and each of them supports a subset of the
following operations:
Operation Example
Read *it
Write *it =
Increment it++
Decrement it–
Comparison it1 < it2
Random access it + 4 , it[2]
9/69