The Standard Library (STL) · 9 min read · Intermediate
std::vector, Maps & STL Algorithms
Replace manual C arrays with std::vector, range-based for loops, and expressive <algorithm> helpers.
What you'll learn
- Grow and iterate over dynamic arrays with std::vector and push_back
- Store key-value pairs in std::unordered_map
- Sort and transform ranges with std::sort and lambda expressions
The Standard Template Library (STL) is C++‘s superpower. std::vector<T> stores elements contiguously in memory (just as fast as a raw C array), while automatically resizing and freeing its own memory.
Range-Based for + <algorithm>
Combining range-based for (const auto& x : items) with algorithms like std::sort, std::count_if, and lambdas ([](int n) { return n >= 80; }) eliminates off-by-one loop bugs.
Fill-in-the-Blank Exercise
Append & Iterate std::vector
Add 95 to the end of scores and loop over every element.
std::vector<int> scores = {80, 90};
scores.(95);
for (int s scores) {
std::cout << s << " ";
}Interactive Code Lab
C++stl_vector.cpp
Expected OutputClick “Run Live” to compile with GCC 13 (C++20)
Lowest: 72 | Highest: 95 Scores >= 85: 3
Check Your Understanding
Score: 0 / 1
1. Which STL container should be your default choice for a dynamic sequence of elements?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.