CodeRigor

FREE COURSE / REAL PRACTICE

Containers & views (C++)

Choose C++ containers and views by storage, lookup, ordering, ownership and invalidation contracts.

Free lessons & exercises19 authored lessons9 units

What you’ll work toward

Explain container tradeoffs and test boundary cases instead of choosing a data structure by familiarity alone.

Read an explanation, inspect the diagrams and work through the available checks, debugging exercises and builds. Feedback and prerequisite links guide the next step.

Before you start

This course builds on these skills:

Individual lessons can add prerequisites. The course map shows your actual readiness.

A look inside the first lesson

std::vector, std::span and std::string_view

C++
#include <print>
#include <string>
#include <vector>

int main() {
    std::vector<int> a(3, 1);   // (count, value): three 1s
    std::vector<int> b{3, 1};   // initializer list: the two elements 3 and 1
    std::println("{} {}", a, b);

    // If the braced values can't form an initializer_list<T>, braces fall back
    // to the other constructors: 3 isn't a std::string, so this is (count, value).
    std::vector<std::string> c{3, "x"};
    std::println("{}", c);
}

Your course outline

Follow the lessons in curriculum order. Open a unit to preview the ideas you’ll work with.

UNIT 011 lesson Foundations
  1. LESSON 01.01

    std::vector, std::span and std::string_view

    Own sequences with std::vector, pass them around with std::span and std::string_view, and never let a view outlive what it looks at.

    Open lesson
UNIT 024 lessons Owning things
  1. LESSON 02.01

    Move semantics and value categories

    Know which expressions can be moved from, write correct noexcept move operations, and know when the compiler elides copies for you.

    Open lesson
  2. LESSON 02.02

    Dynamic array (your own vector)

    Separate raw storage from live objects, grow geometrically for amortized O(1) push_back, and keep ownership, aliasing and exceptions straight.

    Open lesson
  3. LESSON 02.03

    Stacks, queues and ring buffers

    LIFO and FIFO, the standard adaptors, and a growable ring buffer with O(1) push and pop at both ends of its life.

    Open lesson
  4. LESSON 02.04

    Hash maps and sets in practice

    Use std::unordered_map and unordered_set for O(1)-average counting, grouping and lookup — and know their traps: operator[] inserts, order is unspecified, the hash decides your complexity.

    Open lesson
UNIT 032 lessons Generic code
  1. LESSON 03.01

    Iterators and ranges basics

    Know what each iterator category promises, make your own container work with std::ranges, and use sentinels, projections and simple views.

    Open lesson
  2. LESSON 03.02

    Linked lists

    Node-based sequences: sentinels that delete edge cases, O(1) splicing, iterative reversal and teardown, fast/slow pointers — and why they lose to vectors on modern hardware.

    Open lesson
UNIT 041 lesson Hashing and heaps
  1. LESSON 04.01

    Binary heaps and priority queues

    Keep the largest element on top of an array-shaped tree: sift up and down in O(log n), build a heap in O(n), and use std::priority_queue without its traps.

    Open lesson
UNIT 052 lessons Compile time and trees
  1. LESSON 05.01

    Flat containers: flat_map and inplace_vector

    Sorted-vector maps for read-mostly data, and fixed-capacity vectors that never allocate — when they beat node-based and heap-based containers, and what they cost.

    Open lesson
  2. LESSON 05.02

    Binary trees and binary search trees

    Traverse trees in every order, keep the BST invariant through insert and erase, and never let a degenerate tree overflow your stack.

    Open lesson
UNIT 061 lesson Sum types, contracts, graphs
  1. LESSON 06.01

    Balanced search trees

    Keep a search tree's height logarithmic with rotations: AVL balance factors, red-black colours, and why std::map is built the way it is.

    Open lesson
UNIT 073 lessons Pipelines and memory
  1. LESSON 07.01

    Ranges: views and pipelines

    Compose lazy views with |, materialise with ranges::to, fold, generate with std::generator, and know where views dangle or cache.

    Open lesson
  2. LESSON 07.02

    Allocators and std::pmr

    Control where containers get their memory: the allocator model, propagation traits, polymorphic memory resources, arenas, and the lifetime rules that keep them safe.

    Open lesson
  3. LESSON 07.03

    Fixed-capacity containers: no hidden allocations

    Game- and embedded-style containers in the spirit of EASTL: inline storage with an optional heap overflow, small-buffer optimisation, fixed node pools — and why a frame budget bans surprise allocations.

    Open lesson
UNIT 083 lessons Standard containers: contracts, bits & duplicate keys
  1. LESSON 08.01

    Standard containers: identity, transfer & invalidation

    Apply list splice, forward_list erasure, map node handles and borrowed array views; choose storage by ownership and stability guarantees.

    Open lesson
  2. LESSON 08.02

    Packed booleans: vector<bool>, proxies & bitset

    Distinguish bit proxies from boolean values, use fixed-width bit operations beyond 64 bits, and preserve snapshots, bounds and subset-sum semantics.

    Open lesson
  3. LESSON 08.03

    Duplicate keys: ordered, hashed & flat containers

    Use multimap/multiset and unordered multi variants, plus flat_set/flat_multiset/flat_multimap; preserve multiplicity, deterministic results and sorted invariants.

    Open lesson
UNIT 092 lessons Shaped views & stable block storage
  1. LESSON 09.01

    mdspan: shape, strides & borrowed multidimensional data

    Use actual C++23 mdspan with right, left and padded layouts; validate backing spans and overflow, distinguish logical size from storage, and transpose safely.

    Open lesson
  2. LESSON 09.02

    hive storage: stable entities with the reference implementation

    Practice stable insertion and erasure using pinned plf::hive, with a consistent id index and lifetime checks. Native std::hive remains unavailable here.

    Open lesson