Back to all DSA books

Data structures
and algorithms
in C++.

C++ for DSA, complete edition
C++ for DSA book cover

Learn the algorithms in the language interviews and contests still reach for.

Every example is a complete C++20 program that builds with GCC, Clang or Visual Studio using only the standard library: no bits/stdc++.h, no macros, no contest shortcuts. Alongside the classic data structures and algorithms, the book covers the C++ details that turn a correct idea into a crash: unsigned sizes, integer overflow, operator[] quietly inserting keys, iterator invalidation and who owns each node.

PDF, 73 pages, 1.1 MB. Free with Google sign-in.

Written for C++, not translated into it.

What’s inside.

Each chapter has a complete program to run, the time and memory cost, and exercises.

  1. Install a C++ compiler and prepare your workspace

    Set up GCC, Clang or Visual Studio, compile with warnings on, and run your first program.

  2. C++ foundations for DSA

    Enough C++ for your first algorithms: types, functions, loops, vectors and unsigned sizes.

  3. Vectors, arrays, maps and sets

    The everyday containers, what each operation costs, and what operator[] does to a map.

  4. Strings, bytes and characters

    Why std::string holds bytes, and how to handle text without splitting a character.

  5. Structs, references and pointers

    Value semantics, references, pointers and default comparisons for your own types.

  6. Recursion

    Base cases, smaller problems, and the fixed stack that every call uses.

  7. Stacks and queues

    std::stack and std::queue, why pop() returns nothing, and a bracket checker.

    See stacks and queues visualized
  8. Linked lists

    Build a list from unique_ptr, relink it safely, and free a million nodes without a crash.

    See linked lists visualized
  9. Hash tables and unordered_map

    How a key finds its bucket, a small chained table, and the cost of a lookup.

    See hash tables visualized
  10. Trees and binary search trees

    Insert, search and traverse a BST, and compare it with std::set.

    See trees visualized
  11. Heaps and priority queues

    Write a min-heap, then use std::priority_queue with the right comparator.

    See heaps visualized
  12. Searching and sorting

    lower_bound without off-by-one errors, merge sort, and comparators that are strict.

  13. Two pointers, sliding windows and prefix sums

    Three ways to stop rescanning elements, with half-open ranges and sums that do not overflow.

  14. Divide and conquer and backtracking

    Count inversions by splitting, and search choices while undoing each one.

  15. Graph traversal with BFS and DFS

    Adjacency lists, BFS shortest paths, iterative DFS and connected components.

    See graph traversal visualized
  16. Greedy algorithms

    When the locally best choice is optimal, and a counterexample when it is not.

  17. Dynamic programming

    State, recurrence and base cases, with sentinels that cannot overflow.

  18. Mixed practice and a small C++ project

    Choose a technique without a chapter title to help, and build a study planner.

Prefer another language? Go for DSA, JavaScript for DSA, Python for DSA or Java for DSA.