The C++ Standard Template Library (STL) is the part of the C++ standard library built around generic containers, iterators and algorithms. Containers such as std::vector and std::map store data, iterators give a uniform way to walk through them, and algorithms such as std::sort and std::find work on any range of iterators.
Since C++20 the same ideas also appear as ranges, views and concepts. Strictly, "STL" is a historical name; the standard calls these parts the containers, iterators, algorithms and function objects libraries, but developers still use STL for all of them.
| Part | What it is | Examples |
|---|---|---|
| Containers | Types that own and organise elements | vector, array, deque, list, map, set, unordered_map |
| Iterators | Generalised pointers into a container | begin(), end(), std::back_inserter |
| Algorithms | Function templates working on ranges | sort, find, transform, accumulate |
| Function objects | Callable objects, including lambdas | std::less, std::function, lambdas |
Because algorithms depend only on iterators, one std::sort works on a std::vector, a std::deque or a plain array, and you write each algorithm only once.
Containers fall into groups with different performance guarantees.
std::vector (contiguous, the default choice), std::array (fixed size), std::deque, std::list and std::forward_list.std::set, std::map, std::multiset, std::multimap. Lookup is O(log n).std::unordered_set, std::unordered_map. Lookup is O(1) on average.std::stack, std::queue, std::priority_queue.std::flat_map and std::flat_set store sorted keys in contiguous memory, and std::mdspan views multidimensional data.Start with std::vector. Its contiguous layout is cache friendly and beats std::list for most real workloads, even when you insert in the middle.
An iterator points at an element and moves with ++. The standard defines categories by capability: input, output, forward, bidirectional, random access, and since C++20 contiguous. std::vector and std::deque provide random access, std::list is bidirectional, and std::forward_list is forward only.
Modifying a container can invalidate its iterators. For example, push_back on a std::vector may reallocate and invalidate every iterator and reference into it. Check the invalidation rules before storing iterators.
The <algorithm> and <numeric> headers hold more than 100 algorithms. They accept a callable, usually a lambda, which has replaced hand-written functor classes in most code.
The sorted vector is 1, 1, 3, 4, 5, 9, doubling gives a total of 46, and 9 doubled is 18, so the search succeeds.
A functor is any class that overloads operator(). It still works, and std::less<> or std::greater<> are standard ones, but a lambda is shorter and can capture local variables.
C++20 adds the ranges library in <ranges>. Algorithms in std::ranges take a whole container instead of two iterators, and views compose lazily.
Views do no work until you iterate them, and they do not copy the underlying data. Prefer the std::ranges algorithms in new code because they are constrained by concepts and give clearer errors.
Not exactly. The STL refers to the containers, iterators, algorithms and function objects. The standard library is larger and also includes streams, strings, threads, filesystem and more.
std::vector. Switch to std::unordered_map for key lookup, std::map when you need sorted keys, and std::deque when you push often at both ends.
std::map is ordered and lookup takes O(log n). std::unordered_map is hashed, lookup is O(1) on average, and iteration order is unspecified.
They are rarely slower, and they state intent clearly. Many standard library implementations also optimise them, and in C++17 and later you can pass an execution policy such as std::execution::par for parallel versions.
The STL combines containers, iterators, algorithms and function objects so you can write each piece once and reuse it with any type. Default to std::vector, learn the iterator invalidation rules, use lambdas with <algorithm>, and use C++20 ranges for new code.