Skip to content

appleweiping/cs106l

Repository files navigation

Stanford CS106L — Standard C++ Programming (Solutions)

Complete, from-skeleton solutions to all seven assignments of CS106L — Standard C++ Programming (Stanford University), part of a csdiy.wiki full-catalog build. Every assignment is implemented to spec and passes the course's own autograder.

status language license

Overview

CS106L is Stanford's companion course to CS106B that explores modern C++ in depth: streams, references and initialization, STL containers and iterators, classes and const-correctness, templates, lambdas, operator overloading, special member functions, move semantics, std::optional, and RAII with smart pointers. This repository contains an independent implementation of the seven weekly assignments from the official course assignments repo (Spring 2026 offering), built from the official starter skeleton and verified against the provided autograders on Windows with MSYS2 g++ 14.2 (-std=c++20/-std=c++23).

Results (measured on Windows 11, MSYS2 GCC 14.2, CPU-only)

Every assignment passes its official autograder. Raw captured output for each is in results/.

Assignment Topic What it implements Result (measured)
A0 Setup Environment Toolchain check autograder 3/3 (compiler, git, python)
A1 SimpleEnroll Streams, references CSV parse + filtered writes (parse_csv, write_courses_offered/not_offered) autograder 2/2
A2 Marriage Pact Containers, pointers std::set of applicants, std::queue<const std::string*> of initials-matches autograder 2/2
A3 Make a Class Classes, const-correctness BankAccount (2 ctors, private field + method, const getter, setter) CastXML autograder 6/6
A4 Ispell STL algorithms, ranges, templates tokenize (STL, no loops) + spellcheck (ranges views, Damerau-Levenshtein) autograder 4/4, 7/7 gold texts
A5 Treebook SMFs, operator overloading User destructor, copy ctor/assign, deleted moves, operator<< += < autograder 8/8 (incl. leak checks)
A6 ExploreCourses std::optional, monads find_coursestd::optional<Course>, transform/value_or (no conditionals) autograder 2/2
A7 Unique Pointer RAII, move semantics, templates move-only cs106l::unique_ptr<T> + recursive create_list autograder 6/6

Total: 30/30 graded checks across the assignments (plus the 3/3 setup check).

A few concrete measured samples:

  • A4 Ispell loads a 464,811-word dictionary and correctly flags/suggests: mispelled → {dispelled, misspelled}, vilage → {milage, pilage, silage, viage, village, vinage, visage, volage}. With --profile on examples/(marquez).txt (84 tokens): tokenizing input 0.14 ms, dictionary tokenization ~795 ms for 466,550 tokens, spellcheck brute-forcing the full dictionary per token. (See results/assignment4_sample_run.txt.)
  • A7 unique_ptr builds a linked list, prints it, and the RAII destructor chain frees every node in order — verified leak-free by the autograder's custom operator new/delete accounting. (See results/assignment7_linked_list_run.txt.)

Implemented assignments

  • A0 — Assignment Setup — environment/toolchain sanity check.
  • A1 — SimpleEnroll — parse ExploreCourses CSV with streams; write the offered vs. not-offered courses, removing offered ones from the working vector.
  • A2 — Marriage Pact — read applicants into a std::set; find everyone sharing your initials into a std::queue of pointers (no copies); pick a match.
  • A3 — Make a Class — a BankAccount class meeting all six requirements (parameterized + default constructors, private field, private method, const getter, setter); const-correct.
  • A4 — Ispelltokenize using only STL algorithms (std::transform with overlapping ranges, std::inserter, std::erase_if; no for/while loops) and spellcheck using the C++20 ranges/views library.
  • A5 — Treebook — extend a User class with a friend operator<<, a destructor, copy constructor and copy assignment (deep-copying the raw _friends array), deleted move operations, and member operator+= / operator<.
  • A6 — ExploreCourses — change find_course to return std::optional<Course> and build the output with monadic operations (transform + value_or) with no if statements.
  • A7 — Unique Pointer — implement a move-only cs106l::unique_ptr<T> (raw pointer member, operator* -> bool, RAII destructor, deleted copy, move ctor/assignment with self-assignment guard) plus create_list.

Project structure

cs106l/
├── assignment-setup/     # A0 environment check (starter, no code needed)
├── assignment1/          # SimpleEnroll     → main.cpp
├── assignment2/          # Marriage Pact    → main.cpp, short_answer.txt
├── assignment3/          # Make a Class     → class.h, class.cpp, sandbox.cpp, short_answer.txt
├── assignment4/          # Ispell           → spellcheck.cpp
├── assignment5/          # Treebook         → user.h, user.cpp
├── assignment6/          # ExploreCourses   → main.cpp
├── assignment7/          # Unique Pointer   → unique_ptr.h, main.cpp, short_answer.txt
├── results/              # captured autograder output + sample runs (evidence)
├── run_autograder.sh     # convenience runner (see Verification)
├── LICENSE               # MIT (covers this repo's own code only)
└── README.md

Each assignment*/autograder/ holds the course's Python autograder; it creates its own virtualenv at runtime (git-ignored).

How to run

Toolchain: MSYS2 g++ 14.2 on PATH (C++17/20/23) and Python 3.8+.

Compile and run any assignment (Windows note: the executable is main.exe):

cd assignment1
g++ -static-libstdc++ -std=c++20 main.cpp -o main   # A6 uses -std=c++23; A3/A4/A5 add class.cpp/spellcheck.cpp/user.cpp
./main            # runs the code, then the autograder

Per-assignment compile commands:

# A1  g++ -static-libstdc++ -std=c++20 main.cpp -o main
# A2  g++ -static-libstdc++ -std=c++20 main.cpp -o main
# A3  g++ -static-libstdc++ -std=c++20 main.cpp class.cpp -o main
# A4  g++ -static-libstdc++ -std=c++20 main.cpp spellcheck.cpp -o main
# A5  g++ -static-libstdc++ -std=c++20 main.cpp user.cpp -o main
# A6  g++ -static-libstdc++ -std=c++23 main.cpp -o main
# A7  g++ -static-libstdc++ -std=c++20 main.cpp -o main

Or use the helper (handles venv, UTF-8 console, and compile command):

./run_autograder.sh assignment4

Try the A4 spellchecker directly:

cd assignment4 && g++ -static-libstdc++ -std=c++20 main.cpp spellcheck.cpp -o main
./main --unstyled "This string is mispelled"
Get-Content "examples/(marquez).txt" | ./main --stdin --unstyled --profile   # PowerShell

Verification

Each assignment ships with the course's own autograder (a C++ main that runs the assignment, then invokes a Python grader under autograder/). All were run on this machine and their output captured to results/:

  • A1, A2, A6 — compare generated output files / program output against the course's reference answers.
  • A4 — statically checks that tokenize/spellcheck use the required STL/ranges functions and contain no loops, then diffs the spellcheck output against 7 gold-standard literary texts (Kafka, Márquez, Morrison, Melville, Orwell, Tolstoy, plus a gibberish file).
  • A3 — uses CastXML + pygccxml to inspect the class's AST and confirm all six structural requirements.
  • A5, A7 — override global operator new/delete to detect memory leaks while exercising the special member functions and operators.

Environment notes (Windows + GCC 14.2)

Getting the course autograders to run under MSYS2 GCC 14.2 on a Chinese (GBK) Windows console required three environment adjustments. None of them modify course or autograder source code — they only adjust how the tools are invoked:

  1. UTF-8 console. The autograder prints emoji; on a GBK console its C++→Python system() call dies with UnicodeEncodeError. Running the Python grader directly with PYTHONUTF8=1 / PYTHONIOENCODING=utf-8 fixes it.
  2. A3 CastXML vs. GCC 14.2 headers. CastXML's bundled Clang cannot parse GCC 14.2's <bits/c++config.h> (typedef __decltype(0.0bf16) __bfloat16_t; guarded by __BFLT16_DIG__). A venv sitecustomize.py monkeypatches pygccxml to pass -U__BFLT16_DIG__ to CastXML's Clang so that block is skipped.
  3. A3 pygccxml relinking. pygccxml 2.5.0's _relink_declarated_types cannot relink some standard-library template typedefs named type (from <type_traits>, pulled in by <string>). The same sitecustomize.py extends pygccxml's existing skip-list to tolerate them — pure std-library noise, never the student's class, which is located separately.

run_autograder.sh documents and applies (1); (2)/(3) are a one-time sitecustomize.py in assignment3/autograder/Lib/site-packages/ (git-ignored with the rest of the venv).

Tech stack

  • C++20 / C++23 (MSYS2 GCC 14.2): STL containers, iterators, algorithms, the ranges/views library, templates, lambdas, operator overloading, special member functions, move semantics, std::optional, RAII / smart pointers.
  • Python 3 — the course autograders (colorama; CastXML + pygccxml for A3).

Key ideas / what I learned

  • Modeling records with plain structs and driving I/O entirely through std::stringstream/std::ifstream/std::ofstream (A1).
  • Storing non-owning const T* in containers to avoid copies, and reasoning about the resulting lifetime constraints (A2).
  • Writing const-correct classes and understanding what the compiler enforces (A3).
  • Expressing loops as declarative STL/ranges pipelines — std::transform over overlapping iterator ranges, filter/transform views, materializing lazy views into containers without C++23's std::ranges::to (A4).
  • The full set of special member functions and the rule-of-five: deep copies, deleted moves, self-assignment guards, and leak-free ownership (A5, A7).
  • std::optional and its monadic interface (transform/and_then/or_else/ value_or) as a branch-free way to handle "maybe a value" (A6).
  • RAII and move-only ownership by building unique_ptr from scratch, and why a moved-from pointer must be nulled to avoid double-free (A7).

Credits & license

Based on the assignments of CS106L — Standard C++ Programming by Stanford University (assignments by Fabio Ibanez, Haven Whitney, and Jacob Roberts-Baca). Original starter code and specifications belong to their authors; the official assignment repository is at https://github.com/cs106l/cs106l-assignments. This repository is an independent educational reimplementation. Original code here is released under the MIT License.

About

Solutions to Stanford CS106L Standard C++ Programming assignments — modern C++ (STL, RAII, templates, iterators, move semantics) with tests passing

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages