C++ · Module 13 · Lesson 12 min read

STL Overview: Containers, Iterators, Algorithms

The three pillars

PartWhat it isExamples
Containersstore datavector, list, map, set, unordered_map
Iteratorspoint into containers, move through thembegin(), end(), ++it, *it
Algorithmsoperate on ranges of iteratorssort, 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

FamilyContainersOrganised by
Sequencearray, vector, deque, list, forward_listposition
Ordered associativeset, map, multiset, multimapsorted key (tree)
Unordered associativeunordered_set, unordered_map, …hash of key
Adaptorsstack, queue, priority_queuerestricted 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 element
  • end() points one past the last (never dereference it)
  • Empty container: begin() == end()

Iterator categories (weakest → strongest)

CategoryCan doExample
Input / Outputread/write once, move forwardstream iterators
Forwardmulti-pass forwardforward_list, unordered containers
Bidirectional++ and --list, set, map
Random accessit + n, it[n], <vector, deque, array
Contiguouselements adjacent in memoryvector, 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

C++
#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
*/