CppSTL Overview

STL Overview

The Standard Template Library (STL) is the part of the C++ standard library that provides ready-made, highly optimized generic containers, algorithms, and the iterators that connect them. It is, without exaggeration, the most important part of the C++ standard library for everyday programming — nearly every modern C++ codebase leans on it heavily, and understanding it well is what separates idiomatic C++ from C-with-classes.
The four components of the STL
The STL is traditionally described as four cooperating pieces. All of them are built using templates, which is exactly why the same std::sort algorithm works on a vector<int>, a vector<std::string>, or a plain C array — with zero code duplication.

Component

Purpose

Examples

Containers

Store and organize collections of objects

vector, list, deque, set, map, unordered_map, stack, queue

Algorithms

Operate on ranges of elements via iterators

sort, find, count, accumulate, transform, copy

Iterators

A uniform way to traverse any container

begin()/end(), forward/bidirectional/random-access iterators

Function objects (functors)

Encapsulate an operation as an object; customize algorithm behavior

std::less, std::greater, lambdas passed to algorithms

Why iterators matter
The reason algorithms and containers can be mixed and matched so freely is that algorithms never talk to containers directly — they operate on a pair of iterators marking a range's beginning and end. A container just needs to expose begin() and end(), and any generic algorithm can walk across it.

stl_glue.cpp

CPP
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numbers = {5, 3, 8, 1, 9, 2};

    // std::sort doesn't know anything about "vector" specifically —
    // it only needs a begin/end iterator pair.
    std::sort(numbers.begin(), numbers.end());

    for (int n : numbers) {
        std::cout << n << " ";
    }
    std::cout << std::endl; // 1 2 3 5 8 9

    auto it = std::find(numbers.begin(), numbers.end(), 8);
    if (it != numbers.end()) {
        std::cout << "Found 8!" << std::endl;
    }

    return 0;
}
Why prefer the STL over hand-rolled code
  • STL containers and algorithms are written and tuned by compiler and library implementers, and are far more likely to be correct and efficient than a quick hand-rolled linked list or bubble sort.

  • They are extensively tested and used by essentially every C++ program in existence — bugs are rare and well understood.

  • Code that uses std::vector and std::sort communicates intent clearly to other C++ programmers; a hand-rolled dynamic array does not.

  • The STL is part of the standard — no external dependency required, and it works the same way across compilers and platforms.

Don't reinvent std::vector or std::sort
A useful rule of thumb for C++ programmers at every level: before writing your own data structure or algorithm, check whether the STL already provides it. Writing your own dynamic array, linked list, or sorting routine is a great learning exercise, but it's almost never the right choice in real production code — the STL version will be faster, more correct, and more familiar to everyone who reads your code afterward.
What's coming next
The following pages walk through the most commonly used STL containers in depth: vector, list and deque, set and map, the unordered (hash-based) containers, the container adaptors stack, queue, and priority_queue, and the utility types pair and tuple. Later pages cover iterators, algorithms, and functors in more detail.