Standard Template Library (STL) in C++
The Standard Template Library (STL) is a powerful collection of ready-to-use, generic classes and functions built into C++. It provides efficient implementations of common data structures (like arrays, lists, and maps) and algorithms (like sorting and searching), all built using templates so they work with any data type.
Why Use STL?
- Ready-made: No need to write data structures from scratch.
- Efficient: STL containers and algorithms are optimized and tested.
- Generic: Works with any data type thanks to templates.
- Standardized: Available on every C++ compiler without external libraries.
Main Components of STL
| Component | Description | Examples |
|---|---|---|
| Containers | Data structures that store collections of objects | vector, list, map, set, queue, stack |
| Iterators | Objects to traverse containers | begin(), end(), iterator |
| Algorithms | Functions to operate on containers | sort(), find(), count(), reverse() |
| Function objects | Objects that act like functions (functors) | greater<T>, less<T> |
STL Container Categories
Sequence Containers — Elements in linear order:
vector— Dynamic resizable arraylist— Doubly linked listdeque— Double-ended queuearray— Fixed-size array (C++11)
Associative Containers — Elements sorted by key:
map— Key-value pairs, sorted by keyset— Unique values, sorted automaticallymultimap— Like map, but allows duplicate keysmultiset— Like set, but allows duplicates
Unordered Containers — Hash-based, faster lookup (C++11):
unordered_mapunordered_set
Container Adaptors — Built on top of other containers:
stack— LIFO structurequeue— FIFO structurepriority_queue— Sorted access
Quick Overview — Using STL Containers
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> nums = {5, 2, 8, 1, 9, 3};
// Sort using STL algorithm
sort(nums.begin(), nums.end());
// Print using iterator
for (int n : nums) {
cout << n << " ";
}
cout << endl;
// Find element
auto it = find(nums.begin(), nums.end(), 8);
if (it != nums.end())
cout << "Found: " << *it << endl;
return 0;
}
Output:
1 2 3 5 8 9
Found: 8Common STL Algorithms
| Algorithm | Header | Description |
|---|---|---|
sort() | <algorithm> | Sort elements |
find() | <algorithm> | Find first matching element |
count() | <algorithm> | Count occurrences |
reverse() | <algorithm> | Reverse elements |
min_element() | <algorithm> | Find minimum element |
max_element() | <algorithm> | Find maximum element |
accumulate() | <numeric> | Sum of elements |
fill() | <algorithm> | Fill with a value |
binary_search() | <algorithm> | Check if value exists (sorted) |
Iterators
Iterators are like smart pointers that allow traversal over containers regardless of their internal structure:
vector<int> v = {10, 20, 30};
// Using iterator explicitly
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
Output:
10 20 30Key Takeaways
- STL provides ready-made, efficient, generic data structures and algorithms.
- STL has three main parts: containers, iterators, and algorithms.
- Containers store data; iterators traverse it; algorithms operate on it.
- STL greatly reduces the amount of code needed for common programming tasks.
- Always include the appropriate header for each STL component.
