CHAPTER 22
Beginner
Standard Template Library (STL)
Updated: May 17, 2026
5 min read
# CHAPTER 22
Standard Template Library (STL)
1. Introduction
If you need a dynamic array in C++, you *could* write a class, manage pointers, usenew and delete, and manually resize memory when it gets full. OR... you could just use std::vector. The Standard Template Library (STL) is a massive collection of pre-written, highly optimized data structures (Containers) and Algorithms. It is the most powerful tool in a C++ developer's arsenal.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the three core components of the STL: Containers, Iterators, and Algorithms.
-
Use
std::vectorfor dynamic arrays.
-
Use
std::mapandstd::setfor associative data.
- Navigate containers using Iterators.
-
Apply STL Algorithms like
sort().
3. Core Components of STL
- 1. Containers: Objects that store data (e.g., Vector, List, Map).
- 2. Algorithms: Functions that process data (e.g., sort, search, reverse).
- 3. Iterators: Special pointers used to navigate through Containers safely.
4. Vector (Dynamic Array)
Avector is an array that automatically grows in size when you add elements to it.
*(Requires #include <vector>)*
cpp
5. Iterators
Iterators act like pointers designed specifically to step through STL containers.
cpp
6. Map (Key-Value Pairs)
Amap stores data in Key-Value pairs (like a Dictionary in Python). Keys must be unique. It automatically sorts the data by the Keys!
*(Requires #include <map>)*
cpp
7. Set (Unique Elements)
Aset stores only UNIQUE elements and automatically sorts them in ascending order.
*(Requires #include <set>)*
cpp
8. STL Algorithms
The<algorithm> library contains dozens of functions to manipulate containers.
cpp
9. Memory-Level Explanation (vector)
How does a vector grow dynamically?
- 1. It starts with a fixed capacity on the Heap (e.g., 4 elements).
- 2. When you push the 5th element, it allocates a NEW block of memory *double* the size (8 elements).
- 3. It copies the old elements to the new block.
- 4. It deletes the old block.
v.reserve(100)).*
10. Common Mistakes
-
Accessing out-of-bounds with
[ ]:v[10]on a 5-element vector will corrupt memory. Usev.at(10)instead; it throws a safestd::outofrangeexception!
- Modifying a map/vector while iterating: Adding or removing elements while looping through a container with an iterator can invalidate the iterator and crash the program.
11. Exercises
-
1.
Create a
vectorof strings. Ask the user to input 5 names. Usestd::sort()to alphabetize them, then print them.
-
2.
Create a
mapstoring country names as keys and their capitals as values. Look up a capital by passing a country name.
12. MCQ Quiz with Answers
Question 1
Which STL container is best for a dynamically resizing array?
Question 2
Which STL container stores Key-Value pairs?
Question 3
What does a set automatically do?
Question 4
Which function adds an element to the end of a vector?
Question 5
What are Iterators?
Question 6
Which library is required to use the sort() function?
Question 7
How do you access the value in a map's iterator/pair?
Question 8
What is the safer alternative to vector[10]?
Question 9
If a vector runs out of capacity, what does it typically do behind the scenes?
std::sort()?
a) Yes (e.g., sort(arr, arr + size);) b) No, it only works on vectors
Answer: a) Yes (e.g., sort(arr, arr + size);)
13. Interview Questions
-
Q: Explain the difference between
std::vectorandstd::list. When would you use one over the other? (Hint: contiguous memory vs doubly-linked nodes).
-
Q: How is
std::mapimplemented under the hood? (Answer: Red-Black Tree).
- Q: Explain Iterator Invalidation.