Module 8 · The Standard Template Library — topic 1 of 7
STL Overview
The STL is built from containers that hold data, iterators that walk through it and algorithms that process it. Learn how the three fit together and how to choose a container.
Learning objectives
- Name the main container families
- Explain how iterators connect containers and algorithms
- Choose a container from its complexity guarantees
Definition
The Standard Template Library is the part of the C++ standard library consisting of generic containers, iterators, algorithms and function objects, designed so that any algorithm can work with any container through iterators.
Introduction
Before the STL every programmer wrote their own linked lists, sorting routines and hash tables. Alexander Stepanov's STL, standardised in 1998, separated data structures from algorithms: std::sort works on any random-access range, std::find on any range at all.
Why it matters
Using the STL is faster to write, faster to run and less buggy than hand-rolled code.
Competitive programming and interviews assume fluency with vector, map, set, stack, queue and sort.
Detailed explanation
Sequence containers: vector, deque, list, forward_list, array. Associative (sorted, tree-based): set, map, multiset, multimap. Unordered (hash tables): unordered_set, unordered_map. Adaptors: stack, queue, priority_queue.
Iterators are generalised pointers: begin() points at the first element, end() one past the last. Algorithms take a pair [first, last).
Algorithms in <algorithm> and <numeric>: sort, find, count, accumulate, transform, reverse, unique, binary_search, max_element…
Syntax
- Ranges are half-open: [begin, end).
end()must never be dereferenced.- Most algorithms don't change container size.
Code examples
Containers + algorithms
3 7 19 25 42 | sum 96 | >10: 3
One algorithm, different containers
1 0
More examples
Choosing containers for a word index
a: 3 cat: 1 3 dog: 2 ran: 2 3 sat: 1 the: 1 2
Step-by-step explanation
- A
mapkeeps words sorted; each value is asetof line numbers (sorted, no duplicates). index[w]creates an empty set the first time a word appears.insertignores duplicates automatically.- Structured bindings unpack each key/value pair.
Expected output
Running “Choosing containers for a word index” prints exactly:
Practical applications
- Search engines build inverted indexes like this one.
- Compilers keep symbol tables in maps.
- Schedulers use priority queues.
Best practices
- Start with vector; switch only for a measured reason.
- Prefer algorithms over hand-written loops.
- Learn the complexity of each container operation.
Common mistakes
Dereferencing end()
Avoid
Prefer
When nothing is found, find returns end(), which doesn't refer to an element.
Using list for random access
Avoid
Prefer
Lists don't support indexing; use vector when you need random access.
Important notes
- C++20 ranges let you write
std::ranges::sort(v)without begin/end. - Iterators can be invalidated by insertions and deletions; each container documents when.
Key takeaways
- Containers store, iterators traverse, algorithms process.
- Ranges are half-open [begin, end).
- Pick containers by the operations you need.
Try it yourself
- 1Read words until end of input and print each distinct word with its count, alphabetically.
- 2Use
std::max_elementandstd::min_elementon a vector of marks.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
4 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat do the iterators returned by `begin()` and `end()` denote?
- BeginnerWhat is printed?
- ModerateWhy does std::sort not work on a std::list?