ReviseAlgo Logo

Standard Template Library (STL)

STL Overview

Introduction to containers, iterators, algorithms

Interview: Foundation for STL

STL Overview

The Standard Template Library (STL) is C++'s built-in collection of generic, reusable algorithms and data structures. It consists of four main components: Containers (data storage), Iterators (uniform traversal), Algorithms (operations), and Function Objects (customization). The STL philosophy: separate algorithms from containers via iterators.

Container Categories

Sequence Containers

Maintain insertion order. O(1) random access (vector), O(1) front/back (deque).

vectordequelistarray

Associative Containers

Sorted by key (red-black tree). O(log n) operations.

mapsetmultimapmultiset

Unordered Containers

Hash table-based. O(1) average operations.

unordered_mapunordered_set

Container Adaptors

Wrap other containers with restricted interface.

stackqueuepriority_queue

Interview Corner

Q: How do iterators decouple algorithms from containers?

A: STL algorithms accept iterator pairs [begin, end) and operate on whatever they point to. std::sort works on vector iterators, array pointers, and deque iterators equally because they all satisfy the RandomAccessIterator concept. This is generic programming — write once, work with any conforming container.

Best Practices

  • Default to std::vector for sequential data. Specialize to other containers only when profiling reveals a need.
  • Prefer STL algorithms over hand-written loops — they're more expressive, well-tested, and increasingly parallelizable (C++17 execution policies).