Files
lnet_tutor/learning_plans/cpp/01_KNOWLEDGE_GRAPH.md
2025-10-22 20:14:31 +08:00

25 KiB

C++ Knowledge Graph - Complete Dependency Map

🌳 Knowledge Tree Structure

This document maps all C++ concepts with their dependencies, prerequisites, and optimal learning order.


Level 1: Foundation Nodes (No Prerequisites)

1.1 Development Environment

┌──────────────────────────────┐
│ Compiler Installation        │
│ - GCC/G++ (Linux, MinGW)    │
│ - Clang/LLVM (Cross-platform)│
│ - MSVC (Windows)            │
│ - Compiler flags & options  │
└──────────────────────────────┘
        │
        ├─> IDE Setup (VS Code, Visual Studio, CLion, Code::Blocks)
        ├─> Build Tools (Make, CMake, Ninja)
        ├─> Debugging Tools (gdb, lldb, Visual Studio debugger)
        └─> Version Control (Git integration)

1.2 Basic Syntax & Program Structure

┌──────────────────────────────┐
│ Hello World Program          │
│ - #include directive         │
│ - main() function           │
│ - std::cout                 │
│ - Return statement          │
└──────────────────────────────┘
        │
        ├─> Compilation Process (Preprocessor → Compiler → Linker)
        ├─> Header Files (.h vs .hpp)
        ├─> Source Files (.cpp)
        └─> Comments (// vs /* */)

1.3 Fundamental Data Types

┌──────────────────────────────┐
│ Primitive Types              │
│ - int, short, long, long long│
│ - float, double, long double │
│ - char, wchar_t, char16_t    │
│ - bool                       │
│ - void                       │
└──────────────────────────────┘
        │
        ├─> Type Modifiers (signed, unsigned, const, volatile)
        ├─> sizeof Operator
        ├─> Type Limits (#include <climits>, <cfloat>)
        └─> Type Aliases (typedef, using)

1.4 Variables & Constants

┌──────────────────────────────┐
│ Variable Declaration         │
│ - Initialization            │
│ - Assignment                │
│ - Scope (local, global)     │
└──────────────────────────────┘
        │
        ├─> const Variables
        ├─> constexpr (C++11)
        ├─> Literal Constants
        ├─> #define Preprocessor Constants
        └─> auto Type Deduction (C++11)

1.5 Operators

┌──────────────────────────────┐
│ Arithmetic Operators         │
│ Relational Operators         │
│ Logical Operators            │
│ Bitwise Operators            │
│ Assignment Operators         │
└──────────────────────────────┘
        │
        ├─> Operator Precedence
        ├─> sizeof Operator
        ├─> Ternary Operator (?:)
        ├─> Comma Operator
        └─> Type Casting Operators

Level 2: Control Flow (Requires Level 1)

2.1 Conditional Statements

┌──────────────────────────────┐
│ if Statement                 │
│ if-else Statement            │
│ if-else if-else              │
│ Nested if                    │
└──────────────────────────────┘
        │
        ├─> switch Statement
        ├─> case Labels
        ├─> default Case
        ├─> break in switch
        └─> Ternary Operator Alternative

2.2 Loops

┌──────────────────────────────┐
│ while Loop                   │
│ do-while Loop                │
│ for Loop                     │
└──────────────────────────────┘
        │
        ├─> Range-based for Loop (C++11)
        ├─> Nested Loops
        ├─> Loop Control (break, continue)
        ├─> goto Statement (discouraged)
        └─> Infinite Loops

Level 3: Functions (Requires Level 1-2)

3.1 Function Basics

┌──────────────────────────────┐
│ Function Declaration         │
│ Function Definition          │
│ Function Call                │
│ Return Statement             │
└──────────────────────────────┘
        │
        ├─> Parameters & Arguments
        ├─> Pass by Value
        ├─> Default Parameters
        ├─> void Functions
        └─> Function Prototypes

3.2 Advanced Function Concepts

┌──────────────────────────────┐
│ Function Overloading         │
│ Inline Functions             │
│ Recursion                    │
└──────────────────────────────┘
        │
        ├─> constexpr Functions (C++11)
        ├─> consteval Functions (C++20)
        ├─> [[nodiscard]] Attribute (C++17)
        └─> Trailing Return Types (C++11)

Level 4: Arrays & Strings (Requires Level 1-3)

4.1 Arrays

┌──────────────────────────────┐
│ C-style Arrays               │
│ - Declaration & Init        │
│ - Array Indexing            │
│ - Multi-dimensional Arrays  │
└──────────────────────────────┘
        │
        ├─> Array Decay to Pointer
        ├─> Array Size Calculation
        ├─> std::array (C++11)
        └─> Array Initialization Syntax

4.2 Strings

┌──────────────────────────────┐
│ C-style Strings              │
│ - char arrays               │
│ - String literals           │
│ - String manipulation       │
└──────────────────────────────┘
        │
        ├─> std::string Class
        ├─> String Operations
        ├─> std::string_view (C++17)
        ├─> Raw String Literals (C++11)
        └─> User-defined Literals (C++11)

Level 5: Pointers & References (Requires Level 1-4)

5.1 Pointers

┌──────────────────────────────┐
│ Pointer Basics               │
│ - Address-of Operator (&)   │
│ - Dereference Operator (*)  │
│ - nullptr (C++11)           │
└──────────────────────────────┘
        │
        ├─> Pointer Arithmetic
        ├─> Pointers and Arrays
        ├─> Pointer to Pointer
        ├─> void Pointers
        ├─> const Pointers
        ├─> Function Pointers
        └─> Pointer Best Practices

┌──────────────────────────────┐
│ Dynamic Memory               │
│ - new Operator              │
│ - delete Operator           │
│ - new[] & delete[]          │
│ - Memory Leaks              │
└──────────────────────────────┘
        │
        ├─> Placement new
        ├─> Memory Allocation Failures
        ├─> std::nothrow
        └─> RAII Principle

5.2 References

┌──────────────────────────────┐
│ Reference Basics             │
│ - Declaration & Init        │
│ - References vs Pointers    │
│ - const References          │
└──────────────────────────────┘
        │
        ├─> Pass by Reference
        ├─> Return by Reference
        ├─> Reference Members
        ├─> rvalue References (C++11)
        └─> Universal References

┌──────────────────────────────┐
│ Pass by Reference            │
│ Pass by Value                │
│ Pass by Pointer              │
│ When to Use Which            │
└──────────────────────────────┘

Level 6: Object-Oriented Programming (Requires Level 1-5)

6.1 Classes & Objects

┌──────────────────────────────┐
│ Class Definition             │
│ - Data Members              │
│ - Member Functions          │
│ - Access Specifiers         │
│ (public, private, protected)│
└──────────────────────────────┘
        │
        ├─> this Pointer
        ├─> Object Creation
        ├─> Member Initialization
        ├─> Static Members
        ├─> Friend Functions/Classes
        └─> Nested Classes

6.2 Constructors & Destructors

┌──────────────────────────────┐
│ Constructors                 │
│ - Default Constructor       │
│ - Parameterized Constructor │
│ - Copy Constructor          │
│ - Constructor Overloading   │
└──────────────────────────────┘
        │
        ├─> Move Constructor (C++11)
        ├─> Delegating Constructors (C++11)
        ├─> Initializer Lists
        ├─> explicit Keyword
        ├─> Converting Constructors
        └─> Constructor Member Initializer

┌──────────────────────────────┐
│ Destructors                  │
│ - Resource Cleanup          │
│ - Virtual Destructors       │
│ - Rule of Three/Five/Zero   │
└──────────────────────────────┘
        │
        ├─> RAII (Resource Acquisition Is Initialization)
        ├─> Default/Delete (C++11)
        └─> noexcept Destructors

6.3 Operator Overloading

┌──────────────────────────────┐
│ Overloadable Operators       │
│ - Arithmetic (+, -, *, /)   │
│ - Comparison (==, !=, <, >) │
│ - Assignment (=)            │
│ - Stream (<<, >>)           │
└──────────────────────────────┘
        │
        ├─> Member vs Non-member Overloading
        ├─> Subscript Operator []
        ├─> Function Call Operator ()
        ├─> Increment/Decrement (++, --)
        ├─> Conversion Operators
        └─> Spaceship Operator <=> (C++20)

6.4 Inheritance

┌──────────────────────────────┐
│ Single Inheritance           │
│ - Base & Derived Classes    │
│ - Access Control            │
│ - Constructor Chaining      │
└──────────────────────────────┘
        │
        ├─> Multiple Inheritance
        ├─> Virtual Inheritance
        ├─> Diamond Problem
        ├─> protected Members
        ├─> Inheritance Types (public, protected, private)
        └─> Slicing Problem

┌──────────────────────────────┐
│ Polymorphism                 │
│ - Virtual Functions         │
│ - Pure Virtual Functions    │
│ - Abstract Classes          │
└──────────────────────────────┘
        │
        ├─> Virtual Destructors
        ├─> Virtual Function Tables (vtable)
        ├─> Dynamic Binding
        ├─> override Keyword (C++11)
        ├─> final Keyword (C++11)
        └─> Runtime Type Information (RTTI)

Level 7: Templates (Requires Level 6)

7.1 Function Templates

┌──────────────────────────────┐
│ Template Syntax              │
│ - template<typename T>      │
│ - Template Parameters       │
│ - Template Instantiation    │
└──────────────────────────────┘
        │
        ├─> Explicit Instantiation
        ├─> Template Specialization
        ├─> Function Template Overloading
        ├─> Template Argument Deduction
        └─> typename vs class Keyword

7.2 Class Templates

┌──────────────────────────────┐
│ Template Classes             │
│ - Member Functions          │
│ - Static Members            │
│ - Template Methods          │
└──────────────────────────────┘
        │
        ├─> Class Template Specialization
        ├─> Partial Specialization
        ├─> Non-type Template Parameters
        ├─> Default Template Arguments
        ├─> Template Template Parameters
        └─> Friend Templates

7.3 Advanced Templates

┌──────────────────────────────┐
│ Variadic Templates (C++11)   │
│ - Parameter Packs           │
│ - Pack Expansion            │
│ - Fold Expressions (C++17)  │
└──────────────────────────────┘
        │
        ├─> SFINAE (Substitution Failure Is Not An Error)
        ├─> std::enable_if
        ├─> Type Traits
        ├─> if constexpr (C++17)
        ├─> Concepts (C++20)
        └─> Requires Clauses (C++20)

Level 8: Modern C++ Features (Requires Level 1-7)

8.1 Smart Pointers (C++11)

┌──────────────────────────────┐
│ std::unique_ptr              │
│ - Exclusive Ownership       │
│ - Move-only Semantics       │
│ - Custom Deleters           │
└──────────────────────────────┘
        │
        ├─> std::shared_ptr
        ├─> std::weak_ptr
        ├─> make_unique (C++14)
        ├─> make_shared
        ├─> Circular Reference Problem
        └─> When to Use Which

8.2 Move Semantics (C++11)

┌──────────────────────────────┐
│ lvalue vs rvalue             │
│ rvalue References (&&)       │
│ std::move                    │
│ std::forward                 │
└──────────────────────────────┘
        │
        ├─> Move Constructor
        ├─> Move Assignment Operator
        ├─> Perfect Forwarding
        ├─> Reference Collapsing
        ├─> Universal References
        └─> Return Value Optimization (RVO, NRVO)

8.3 Lambda Expressions (C++11)

┌──────────────────────────────┐
│ Lambda Syntax                │
│ - [capture](params){}       │
│ - Capture Clauses           │
│ - Return Type Deduction     │
└──────────────────────────────┘
        │
        ├─> Capture Modes ([=], [&], [this], [*this])
        ├─> Generic Lambdas (C++14)
        ├─> Init Capture (C++14)
        ├─> constexpr Lambdas (C++17)
        ├─> Template Lambdas (C++20)
        └─> Lambda as Function Objects

8.4 Standard Library Enhancements

┌──────────────────────────────┐
│ C++11 Features               │
│ - auto, decltype            │
│ - nullptr                   │
│ - constexpr                 │
│ - Range-based for          │
└──────────────────────────────┘
        │
        ├─> C++14: Generic lambdas, digit separators
        ├─> C++17: Structured bindings, optional, variant, any
        ├─> C++20: Concepts, ranges, coroutines, modules
        └─> C++23: std::expected, multidimensional subscript

Level 9: STL - Standard Template Library (Requires Level 7-8)

9.1 Containers

┌──────────────────────────────┐
│ Sequence Containers          │
│ - vector, deque, list       │
│ - forward_list, array       │
└──────────────────────────────┘
        │
        ├─> Associative Containers (set, map, multiset, multimap)
        ├─> Unordered Containers (unordered_set, unordered_map)
        ├─> Container Adaptors (stack, queue, priority_queue)
        ├─> span (C++20)
        └─> Container Selection Guidelines

┌──────────────────────────────┐
│ Container Operations         │
│ - Insertion, Deletion       │
│ - Access, Search            │
│ - Iteration                 │
└──────────────────────────────┘
        │
        ├─> Iterator Invalidation
        ├─> Container Complexity (Big-O)
        └─> Container Performance Characteristics

9.2 Iterators

┌──────────────────────────────┐
│ Iterator Categories          │
│ - Input/Output Iterators    │
│ - Forward/Bidirectional     │
│ - Random Access Iterators   │
└──────────────────────────────┘
        │
        ├─> Iterator Adaptors (reverse, insert, stream)
        ├─> Iterator Traits
        ├─> begin() / end()
        ├─> cbegin() / cend()
        ├─> rbegin() / rend()
        └─> Custom Iterators

9.3 Algorithms

┌──────────────────────────────┐
│ Non-modifying Algorithms     │
│ - find, count, search       │
│ - min, max, accumulate      │
└──────────────────────────────┘
        │
        ├─> Modifying Algorithms (copy, transform, replace)
        ├─> Sorting (sort, stable_sort, partial_sort)
        ├─> Binary Search (lower_bound, upper_bound)
        ├─> Heap Operations
        ├─> Numeric Algorithms
        └─> Ranges (C++20)

9.4 Function Objects & Utilities

┌──────────────────────────────┐
│ Function Objects (Functors)  │
│ - operator() Overloading    │
│ - std::function             │
│ - std::bind                 │
└──────────────────────────────┘
        │
        ├─> Standard Functors (plus, minus, less, greater)
        ├─> Predicate Functions
        ├─> Lambda as Functors
        └─> Function Pointers vs Functors

Level 10: Memory Management (Requires Level 5-8)

10.1 Manual Memory Management

┌──────────────────────────────┐
│ Stack vs Heap                │
│ new & delete                 │
│ Memory Leaks                 │
│ Dangling Pointers            │
└──────────────────────────────┘
        │
        ├─> Memory Alignment
        ├─> Placement new
        ├─> Custom Allocators
        ├─> Memory Pools
        └─> Object Pools

10.2 Advanced Memory Concepts

┌──────────────────────────────┐
│ Memory Order & Layout        │
│ - Structure Padding         │
│ - alignof, alignas          │
│ - Cache-friendly Structures │
└──────────────────────────────┘
        │
        ├─> Memory Profiling Tools
        ├─> Valgrind, AddressSanitizer
        ├─> Memory Leak Detection
        └─> Memory Optimization Techniques

Level 11: Concurrency (Requires Level 1-10)

11.1 Threading Basics

┌──────────────────────────────┐
│ std::thread                  │
│ - Thread Creation           │
│ - Thread Joining            │
│ - Thread Detaching          │
└──────────────────────────────┘
        │
        ├─> Thread Local Storage
        ├─> Thread IDs
        ├─> Hardware Concurrency
        └─> Thread Management

11.2 Synchronization

┌──────────────────────────────┐
│ Mutexes                      │
│ - std::mutex                │
│ - std::recursive_mutex      │
│ - std::timed_mutex          │
└──────────────────────────────┘
        │
        ├─> Lock Guards (std::lock_guard, std::unique_lock)
        ├─> Condition Variables
        ├─> std::atomic
        ├─> Memory Ordering
        ├─> std::future & std::promise
        └─> std::async

┌──────────────────────────────┐
│ Thread Safety                │
│ - Data Races                │
│ - Race Conditions           │
│ - Deadlocks                 │
└──────────────────────────────┘
        │
        ├─> Lock-free Programming
        ├─> Wait-free Programming
        ├─> Compare-and-Swap (CAS)
        └─> Thread-safe Data Structures

Level 12: Exception Handling & Error Management (Requires Level 6)

12.1 Exceptions

┌──────────────────────────────┐
│ try-catch Blocks             │
│ throw Statement              │
│ Standard Exceptions          │
│ Custom Exceptions            │
└──────────────────────────────┘
        │
        ├─> Exception Specifications (deprecated)
        ├─> noexcept (C++11)
        ├─> Exception Safety Guarantees
        ├─> RAII for Exception Safety
        └─> Stack Unwinding

12.2 Error Handling Strategies

┌──────────────────────────────┐
│ Error Codes vs Exceptions    │
│ std::optional (C++17)        │
│ std::expected (C++23)        │
│ std::variant (C++17)         │
└──────────────────────────────┘
        │
        └─> Error Handling Best Practices

🔗 Dependency Map Summary

Critical Learning Path

Level 1 (Basics)
    → Level 2 (Control Flow)
    → Level 3 (Functions)
    → Level 4 (Arrays & Strings)
    → Level 5 (Pointers & References)
    → Level 6 (OOP)
    → Level 7 (Templates)
    → Level 8 (Modern C++) [can parallel with 9-11]
    → Level 9 (STL)
    → Level 10 (Memory)
    → Level 11 (Concurrency) [can parallel with 10]
    → Level 12 (Exceptions)

Parallel Learning Opportunities

  • Levels 8, 9, 10, 11 can be learned in parallel after mastering Level 7
  • Exception handling (Level 12) can be introduced earlier but fully mastered later
  • Modern C++ features can be learned alongside traditional concepts

📊 Prerequisite Matrix

Topic Must Know First Can Learn In Parallel
Pointers Arrays, Functions References
References Pointers basics -
Classes Pointers, References -
Templates Classes, Functions -
Smart Pointers Pointers, RAII, Templates Move Semantics
Move Semantics References, OOP Smart Pointers
STL Templates, OOP Modern C++ Features
Concurrency Pointers, References, OOP Memory Management
Lambdas Functions, OOP Templates

This knowledge graph ensures you build strong foundations before tackling advanced C++ concepts!