The C++ Textbook

A complete, working C++

Every code sample on this site compiles and runs in your browser. Every hard idea — ownership, lifetime, moves, templates — comes with a diagram of what the machine is actually doing, and problems that check your answer by running it.

Start at the beginning Jump to problems

84 chapters · 84 written · 205 problems

Three ways to read this

New to programming

Read Parts 1–4 in order. They assume nothing: no compiler installed, no terminal experience, no maths beyond arithmetic.

Begin →

You know another language

Skim Part 1, then start at the memory model. That is where C++ stops resembling Python or Java and starts asking you to make decisions.

Start with memory →

You want the problems

The problem bank is filterable by topic and difficulty, and each problem compiles and tests your submission. Your progress is saved in this browser.

Open the bank →

Contents

  1. Part 1

    Foundations

    What a program is, what the compiler does with it, and the handful of constructs every later chapter assumes. Nothing here presumes you have programmed before.

    1. 1.1 Hello, machine
    2. 1.2 Values, types, and names
    3. 1.3 Making decisions
    4. 1.4 Repetition
    5. 1.5 Functions
    6. 1.6 Your toolchain
  2. Part 2

    Memory and objects

    The part of C++ that other languages hide from you. Everything from here on — copies, moves, containers, performance — is a consequence of what is in this part.

    1. 2.1 Objects and storage
    2. 2.2 Pointers
    3. 2.3 References
    4. 2.4 Lifetime and scope
    5. 2.5 The stack and the heap
    6. 2.6 Arrays, and why they decay
    7. 2.7 const and constness
  3. Part 3

    Types you make

    How to build a type that behaves as well as a built-in one: it copies correctly, cleans up after itself, and cannot be misused by accident.

    1. 3.1 Structs and classes
    2. 3.2 RAII
    3. 3.3 Copying
    4. 3.4 Moving
    5. 3.5 Rule of zero, three, five
    6. 3.6 Operator overloading
    7. 3.7 Inheritance and virtual functions
    8. 3.8 When not to use inheritance
  4. Part 4

    The standard library

    The containers, algorithms, and utilities that mean you rarely need to write a loop or manage memory by hand.

    1. 4.1 std::string and text
    2. 4.2 Sequence containers
    3. 4.3 Associative containers
    4. 4.4 Iterators
    5. 4.5 Algorithms
    6. 4.6 Ranges and views
    7. 4.7 Smart pointers
    8. 4.8 optional, variant, expected
    9. 4.9 Input, output, formatting
  5. Part 5

    Generic programming

    Writing code once that works for every type it makes sense for, with errors that name the real problem.

    1. 5.1 Function templates
    2. 5.2 Class templates
    3. 5.3 Deduction and forwarding
    4. 5.4 Concepts and constraints
    5. 5.5 Compile-time computation
    6. 5.6 Variadic templates
    7. 5.7 Type traits
    8. 5.8 Static polymorphism
  6. Part 6

    Correctness

    How C++ programs go wrong, and the systematic ways to stop them.

    1. 6.1 Exceptions
    2. 6.2 Error handling without exceptions
    3. 6.3 Undefined behaviour
    4. 6.4 Testing
    5. 6.5 Debugging
    6. 6.6 Invariants and assertions
  7. Part 7

    Performance

    Making programs fast on the machines that exist, based on measurement rather than folklore.

    1. 7.1 What the compiler does
    2. 7.2 Measuring
    3. 7.3 Cache and layout
    4. 7.4 Zero-cost, examined
    5. 7.5 Copies and elision
    6. 7.6 Inlining and linking
  8. Part 8

    Concurrency

    Doing more than one thing at a time without corrupting your data.

    1. 8.1 Threads
    2. 8.2 Races and mutexes
    3. 8.3 Atomics
    4. 8.4 Futures and tasks
    5. 8.5 Coroutines
  9. Part 9

    Building real software

    Everything between a single file that compiles and a project other people can build, depend on, and change.

    1. 9.1 Translation units
    2. 9.2 Modules
    3. 9.3 Build systems
    4. 9.4 Dependencies
    5. 9.5 Project: search index
  10. Part 10

    Problem solving

    The techniques competitive programming is built on, taught against the constraint that decides which one you need — and drilled on a judge.

    1. 10.1 Reading a problem
    2. 10.2 Counting the work
    3. 10.3 Template and fast I/O
    4. 10.4 Sorting and comparators
    5. 10.5 Binary search
    6. 10.6 Two pointers
    7. 10.7 Prefix sums
    8. 10.8 Monotonic stacks
    9. 10.9 Monotonic deques
    10. 10.10 Hashing and counting
    11. 10.11 Recursion
    12. 10.12 Bitmasks
    13. 10.13 Greedy
    14. 10.14 Divide and conquer
    15. 10.15 Meet in the middle
    16. 10.16 Representing graphs
    17. 10.17 BFS
    18. 10.18 DFS
    19. 10.19 Topological order
    20. 10.20 Union-Find
    21. 10.21 Dijkstra
    22. 10.22 Negative weights
    23. 10.23 Spanning trees
    24. 10.24 Tree DP