Lesson 12 • Expert
Standard Template Library
Master vectors, maps, sets, queues, and algorithms — the powerful toolkit that makes C++ development fast and efficient.
What You'll Learn
- ✓ Vectors and maps for data storage
- ✓ Sets, queues, stacks, and priority queues
- ✓ STL algorithms (sort, find, transform)
- ✓ Practical patterns: word counting, deduplication
The STL at a Glance
The STL is a collection of template-based containers and algorithms that ship with every C++ compiler. It's like having a professional toolbox — you don't build a hammer from scratch, you reach for the right tool.
| Container | Type | Analogy | Best For |
|---|---|---|---|
vector | Sequence | Resizable array | Most general use |
map | Associative | Dictionary | Key-value lookups |
set | Associative | Unique collection | No duplicates, fast search |
queue | Adapter | Line at a store | FIFO processing |
stack | Adapter | Stack of plates | LIFO processing |
priority_queue | Adapter | ER triage | Largest/smallest first |
Vectors & Maps
Store, iterate, insert, and erase with vectors and maps
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main() {
// === VECTOR ===
cout << "=== Vector ===" << endl;
vector<string> fruits = {"Apple", "Banana", "Cherry"};
fruits.push_back("Date");
fruits.push_back("Elderberry");
// Iterate
for (const string &f : fruits) {
cout << "- " << f << endl;
}
cout << "Size: " << fruits.size() << endl;
// Insert and erase
fruits.insert(fruits.begin() +
...Sets, Queues & Stacks
Use unique sets, FIFO queues, LIFO stacks, and priority queues
#include <iostream>
#include <set>
#include <queue>
#include <stack>
#include <string>
using namespace std;
int main() {
// === SET — unique, sorted elements ===
cout << "=== Set ===" << endl;
set<int> numbers = {5, 3, 8, 1, 3, 5, 8}; // Duplicates removed!
numbers.insert(4);
numbers.insert(1); // Already exists, ignored
for (int n : numbers) {
cout << n << " "; // Sorted: 1 3 4 5 8
}
cout << endl;
// Check membership
cout << "Contain
...STL Algorithms
Sort, search, transform, count, and deduplicate with algorithms
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
vector<int> nums = {42, 17, 93, 5, 68, 31, 85, 12};
// Sort
sort(nums.begin(), nums.end());
cout << "Sorted: ";
for (int n : nums) cout << n << " ";
cout << endl;
// Sort descending
sort(nums.begin(), nums.end(), greater<int>());
cout << "Descending: ";
for (int n : nums) cout << n << " ";
cout << endl;
// Find
sort(nu
...Practical: Word Counter
Build a word frequency counter and sort by occurrence
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
int main() {
// Practical: Word frequency counter
string text = "the cat sat on the mat the cat likes the mat";
map<string, int> wordCount;
istringstream stream(text);
string word;
while (stream >> word) {
wordCount[word]++;
}
cout << "=== Word Frequencies ===" << endl;
for (const auto &[w, count] : wordCou
...Common Mistakes
⚠️ Iterator invalidation: Modifying a container (insert/erase) while iterating can crash. Use the return value of erase() to continue safely.
⚠️ Using [] on map creates entries: myMap["missing"] creates a default entry. Use .count() or .find() to check existence.
⚠️ Forgetting sort before binary_search: binary_search requires sorted data — unsorted data gives wrong results.
Pro Tips
💡 Use unordered_map for speed: unordered_map uses hashing for O(1) average lookups vs O(log n) for map.
💡 Structured bindings: for (auto &[key, val] : myMap) is the modern way to iterate maps (C++17).
💡 Lambda functions: Use lambdas with STL algorithms: sort(v.begin(), v.end(), [](int a, int b) { return a > b; });
Lesson Complete!
You now have a solid command of the STL — the most important library in C++. Next up: Memory Management — master dynamic allocation with new, delete, and smart pointers.
Sign up for free to track which lessons you've completed and get learning reminders.