Skip to content

Latest commit

 

History

History

03_toolbox

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

The Toolbox - Standard Library & Modern Features

Don't Reinvent the Wheel - Use Battle-Tested Tools


📚 Module Overview

This module covers the third layer of the first-principles C++ learning path: The Toolbox.

After understanding memory control and zero-cost abstractions, you learn how to leverage the massive, optimized C++ ecosystem - the Standard Library and modern language features.


🎯 Learning Objectives

After completing this module, you will understand:

  1. STL Containers - When and how to use each container
  2. STL Algorithms - Write loops once, reuse everywhere
  3. Iterators - The glue between containers and algorithms
  4. Modern C++ Features - auto, lambdas, ranges, modules
  5. Compilation Process - From source to executable
  6. Tool Ecosystem - Build systems, package managers, tooling

📁 Module Structure

03_toolbox/
├── 01_stl/                         # Standard Template Library
│   ├── 01_containers/              # vector, map, set, etc.
│   ├── 02_algorithms/              # sort, find, transform
│   ├── 03_iterators/               # Iterator categories and usage
│   └── 04_functional/              # Lambdas, function objects
│
├── 02_modern_features/             # C++11/14/17/20/23 features
│   ├── 01_auto_type_deduction/     # auto and decltype
│   ├── 02_range_for/               # Modern iteration
│   ├── 03_lambdas/                 # Anonymous functions
│   ├── 04_tuples/                  # Multiple return values
│   ├── 05_optional/                # Optional values
│   ├── 06_variants/                # Type-safe unions
│   ├── 07_string_view/             # Non-owning string references
│   ├── 08_concepts/                # C++20 constraints
│   ├── 09_ranges/                  # C++20 ranges library
│   └── 10_modules/                 # C++20 modules
│
├── 03_compilation/                 # Build & Tooling
│   ├── 01_cmake_basics/            # CMake fundamentals
│   ├── 02_build_systems/           # Make, Ninja
│   ├── 03_linking/                 # Static vs dynamic linking
│   ├── 04_optimization/            # Compiler flags
│   ├── 05_debugging/               # GDB, sanitizers
│   └── 06_package_managers/        # vcpkg, Conan
│
└── README.md                       # This file

📖 Sub-Module Overview

01: STL (Standard Template Library)

Focus: Learn to use the tools millions of developers have optimized

Topics Covered:

Containers (01_containers/)

  • Sequence containers: vector, deque, list, forward_list, array
  • Associative containers: map, set, multimap, multiset
  • Unordered containers: unordered_map, unordered_set
  • Container adapters: stack, queue, priority_queue
  • Choosing the right container - Performance characteristics

Algorithms (02_algorithms/)

  • Non-modifying: find, count, search, equal, mismatch
  • Modifying: copy, move, transform, replace, fill
  • Partitioning: partition, sort, nth_element
  • Sorting: sort, stable_sort, partial_sort
  • Numeric: accumulate, inner_product, adjacent_difference

Iterators (03_iterators/)

  • Iterator categories (input, output, forward, bidirectional, random access)
  • Iterator traits and operations
  • Const iterators vs regular iterators
  • Reverse iterators
  • Custom iterators

Functional (04_functional/)

  • Lambda expressions and captures
  • std::function type erasure
  • std::bind and placeholders
  • Function objects (functors)
  • Predicates and comparators

Key Questions Answered:

  • Which container should I use for X?
  • Why is std::vector faster than arrays in other languages?
  • How do I write algorithms that work on any container?
  • What's the difference between std::sort and std::list::sort?

02: Modern Features

Focus: Language features that make C++ modern and expressive

Topics Covered:

Auto & Type Deduction (01_auto_type_deduction/)

  • auto keyword
  • decltype specifier
  • decltype(auto) for return types
  • Template argument deduction guides
  • When to use auto vs explicit types

Range-based For (02_range_for/)

  • Modern loop syntax
  • Custom iterators for range-for
  • Initialization statements in range-for
  • Ranges views (C++20)

Lambdas (03_lambdas/)

  • Lambda syntax and captures
  • Capture by reference vs value
  • Generic lambdas (C++14)
  • constexpr lambdas (C++17)
  • Lambda as comparator

Tuples (04_tuples/)

  • std::pair and std::tuple
  • Structured bindings (C++17)
  • std::make_tuple
  • Tuple cat and transformation
  • Multiple return values

Optional (05_optional/)

  • std::optional for optional values
  • Avoiding sentinel values
  • Monadic operations (and_then, or_else, transform)
  • std::expected (C++23)

Variants (06_variants/)

  • std::variant - Type-safe unions
  • std::visit pattern
  • std::get and access
  • Recursive variants
  • Sum types

String View (07_string_view/)

  • std::string_view for non-owning references
  • Avoiding string copies
  • std::string vs const char* vs std::string_view
  • Lifetime considerations

Concepts (08_concepts/)

  • C++20 concepts explained
  • template concepts
  • Constrained templates
  • Standard library concepts
  • Better error messages

Ranges (09_ranges/)

  • C++20 ranges library
  • Range views (filter, transform, take)
  • Range algorithms
  • Pipelining operations
  • Lazy evaluation

Modules (10_modules/)

  • C++20 modules vs headers
  • Module import/export
  • Build system implications
  • Migration from headers

Key Questions Answered:

  • How do I write modern, readable C++?
  • What features replace old C-style patterns?
  • How do I avoid boilerplate code?
  • What are the performance implications of modern features?

03: Compilation & Tooling

Focus: Understanding and controlling the build process

Topics Covered:

CMake Basics (01_cmake_basics/)

  • CMakeLists.txt structure
  • Targets and properties
  • Finding packages
  • Cross-platform building
  • Best practices

Build Systems (02_build_systems/)

  • How compilation works
  • Make and Ninja generators
  • Incremental builds
  • Parallel compilation
  • Dependency management

Linking (03_linking/)

  • Static vs dynamic linking
  • Shared libraries (.dll/.so)
  • Order-only dependencies
  • Symbol visibility
  • ODR (One Definition Rule)

Optimization (04_optimization/)

  • Compiler optimization levels (-O0, -O1, -O2, -O3, -Os)
  • Link-time optimization (LTO)
  • Profile-guided optimization (PGO)
  • When to optimize
  • Measuring performance impact

Debugging (05_debugging/)

  • Debug vs Release builds
  • GDB basics
  • AddressSanitizer, MemorySanitizer, UndefinedBehaviorSanitizer
  • Valgrind
  • Debug symbols (-g)
  • Logging and assertions

Package Managers (06_package_managers/)

  • vcpkg (Microsoft's package manager)
  • Conan
  • System package managers
  • FetchContent
  • Choosing dependencies

Key Questions Answered:

  • How do I build a C++ project from scratch?
  • What happens during compilation?
  • How do I optimize my code?
  • How do I find and fix bugs?
  • How do I manage dependencies?

🎓 Learning Path

Prerequisites

Before starting this module, you should have completed:

  • ✅ 01_memory_control - Understanding of memory, pointers, RAII
  • ✅ 02_zero_cost_abstraction - Classes, move semantics, smart pointers
  • Basic C++ syntax familiarity
  • Working development environment

Recommended Order

  1. Start with: 01_stl - The foundation of C++ programming

    • Learn containers → algorithms → iterators → lambdas
  2. Then: 02_modern_features - Write modern C++ code

    • Auto, range-for, lambdas → tuples, optional, variants → advanced features
  3. Finally: 03_compilation - Understand and control your build

    • CMake → linking → optimization → debugging → tooling

Time Investment

Sub-Module Time
01_stl 4-6 hours
02_modern_features 4-6 hours
03_compilation 2-3 hours
Total 10-15 hours

With practice: 3-4 weeks to become proficient.


📊 Container Selection Guide

Decision Tree

Need to store data?
│
├─ Sequential access only?
│  └─ Array with fixed size?
│     ├─ Yes → std::array
│     └─ No → std::deque (if frequent front insertion)
│            └─ std::vector (default choice)
│
├─ Need key-value lookup?
│  └─ Sorted order required?
│     ├─ Yes → std::map / std::set
│     └─ No → std::unordered_map / std::unordered_set (faster)
│
├─ Need priority queue?
│  └─ std::priority_queue
│
└─ Need LIFO / FIFO?
   ├─ std::stack (LIFO)
   └─ std::queue (FIFO)

Container Complexities

Container Random Access Insert Delete Search
vector ✓ O(1) O(n)* O(n)* O(n)
deque ✓ O(1) O(1) O(1) O(n)
list ✗ O(n) ✓ O(1) ✓ O(1) O(n)
map ✓ O(log n) O(log n) O(log n) O(log n)
unordered_map ✓ O(1)** O(1) O(1) O(1)

*Amortized for end, O(n) for middle **Average case, worst O(n)


🛠️ Build and Run

All Lessons

cmake -G Ninja -B build
cmake --build build

Individual Topics

# STL containers
./build/bin/toolbox_stl_containers_*.exe

# STL algorithms
./build/bin/toolbox_stl_algorithms_*.exe

# Modern features
./build/bin/toolbox_modern_*.exe

# Compilation
./build/bin/toolbox_compilation_*.exe

💡 Key Takeaways

STL Philosophy

"The STL is not a collection of classes. It's a collection of algorithms that operate on iterators that traverse containers." - Alexander Stepanov

The magic of STL: Algorithms are decoupled from containers!

// Same algorithm works on ANY container
std::sort(v.begin(), v.end());        // vector
std::sort(l.begin(), l.end());        // list
std::sort(a.begin(), a.end());        // array

Modern C++ Philosophy

C++11 Rule of Thumb:

  • Use auto when type is obvious
  • Use range-for for simple iteration
  • Use lambdas for local functions
  • Use unique_ptr for ownership
  • Use make_shared/make_unique

C++17 Rule of Thumb:

  • Use structured bindings
  • Use std::optional for nullable values
  • Use std::filesystem for paths
  • Use if with initializer

C++20 Rule of Thumb:

  • Use concepts for template constraints
  • Use ranges for pipeline-style algorithms
  • Use std::format instead of iostream
  • Use [[likely]]/[[unlikely]] attributes

C++23 Rule of Thumb:

  • Use std::print instead of std::cout
  • Use Deducing This for member functions
  • Use std::flatten for tuples
  • Use std::expected for error handling

📖 Further Reading

Books

  • "The C++ Standard Library" by Nicolai Josuttis
  • "Effective STL" by Scott Meyers
  • "C++20: The Complete Guide" by Nicolai Josuttis
  • "Professional C++" by Marc Gregoire

Online Resources


✅ Completion Checklist

After completing this module, you should be able to:

STL Skills

  • Choose the right container for any use case
  • Use STL algorithms instead of writing loops
  • Write custom iterators and predicates
  • Use lambdas effectively
  • Understand iterator categories

Modern Features Skills

  • Use auto appropriately
  • Write range-for loops
  • Create and use lambdas
  • Use std::optional, std::variant, std::any
  • Understand C++20 concepts and ranges

Tooling Skills

  • Write a CMakeLists.txt from scratch
  • Debug with sanitizers
  • Profile and optimize code
  • Manage dependencies with vcpkg
  • Understand linking and symbols

🚀 Next Steps

After completing The Toolbox, continue to:

  1. Advanced Topics - Concurrency, networking, graphics
  2. Domain-Specific Learning - GUI, game dev, systems programming
  3. Real Projects - Build something substantial

🎯 Module Philosophy

"Don't reinvent the wheel. The standard library is battle-tested, optimized, and portable."

The toolbox module teaches you to:

  • Leverage existing solutions instead of writing from scratch
  • Write idiomatic C++ that other developers understand
  • Build on proven foundations that work across platforms
  • Stay productive by using high-level abstractions

Remember: The best code is code you don't have to write!


Module Status: 📝 Planned (Not yet implemented) Prerequisites:

  • ✅ 01_memory_control Complete
  • ✅ 02_zero_cost_abstraction Complete

Next Module: Advanced Topics (To be determined)


Last Updated: 2026-02-03 C++ Standard: C++23 Target Compilers: Clang 20.1.0, MSVC 19.4+, GCC 13+