STL Overview: Containers, Iterators, Algorithms
The three pillars
| Part | What it is | Examples |
|---|---|---|
| Containers | store data | vector, list, map, set, unordered_map |
| Iterators | point into containers, move through them | begin(), end(), ++it, *it |
| Algorithms | operate on ranges of iterators | sort, find, count, accumulate |
Key idea: algorithms don't know about containers — they work on iterator ranges [first, last). So one std::find works for vectors, lists, arrays and more. N containers × M algorithms without writing N × M functions.
Container families
| Family | Containers | Organised by |
|---|---|---|
| Sequence | array, vector, deque, list, forward_list | position |
| Ordered associative | set, map, multiset, multimap | sorted key (tree) |
| Unordered associative | unordered_set, unordered_map, … | hash of key |
| Adaptors | stack, queue, priority_queue | restricted interface |
| Views (non-owning) | string_view, span, ranges views | — |
Common member functions
size(), empty(), clear(), begin()/end(), insert(), erase(), swap() — consistent names across containers.
Half-open ranges [begin, end)
begin()points to the first elementend()points one past the last (never dereference it)- Empty container:
begin() == end()
Iterator categories (weakest → strongest)
| Category | Can do | Example |
|---|---|---|
| Input / Output | read/write once, move forward | stream iterators |
| Forward | multi-pass forward | forward_list, unordered containers |
| Bidirectional | ++ and -- | list, set, map |
| Random access | it + n, it[n], < | vector, deque, array |
| Contiguous | elements adjacent in memory | vector, array, string |
std::sort needs random access → you can't std::sort a std::list (it has its own list.sort()).
Headers
<vector> <list> <deque> <map> <set> <unordered_map> <unordered_set> <stack> <queue> <algorithm> <numeric> <iterator>
Example
#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <set>
#include <vector>
int main() {
std::vector<int> v = { 42, 7, 93, 15, 7 };
std::list<int> l(v.begin(), v.end()); // build a list from a vector's range
std::set<int> s(v.begin(), v.end()); // a set keeps unique sorted values
// the SAME algorithm works on different containers through iterators
auto in_vector = std::find(v.begin(), v.end(), 93);
auto in_list = std::find(l.begin(), l.end(), 93);
std::cout << "found 93 in vector at index " << (in_vector - v.begin())
<< ", in list? " << std::boolalpha << (in_list != l.end()) << '\n';
std::cout << "count of 7 in vector: " << std::count(v.begin(), v.end(), 7) << '\n';
std::cout << "sum of list: " << std::accumulate(l.begin(), l.end(), 0) << '\n';
std::cout << "set (sorted, unique):";
for (int x : s) std::cout << ' ' << x;
std::cout << '\n';
// iterating manually
std::cout << "vector via iterators:";
for (auto it = v.begin(); it != v.end(); ++it) std::cout << ' ' << *it;
std::cout << "\nvector backwards:";
for (auto it = v.rbegin(); it != v.rend(); ++it) std::cout << ' ' << *it;
std::cout << '\n';
std::sort(v.begin(), v.end()); // vector: random access
l.sort(); // list: needs its own member sort
std::cout << "sorted vector:";
for (int x : v) std::cout << ' ' << x;
std::cout << "\nsorted list: ";
for (int x : l) std::cout << ' ' << x;
std::cout << '\n';
std::vector<int> empty;
std::cout << "empty.begin() == empty.end()? " << (empty.begin() == empty.end()) << '\n';
return 0;
}
/* Output:
found 93 in vector at index 2, in list? true
count of 7 in vector: 2
sum of list: 164
set (sorted, unique): 7 15 42 93
vector via iterators: 42 7 93 15 7
vector backwards: 7 15 93 7 42
sorted vector: 7 7 15 42 93
sorted list: 7 7 15 42 93
empty.begin() == empty.end()? true
*/