cpp-know-hows

cpp related stuff

View on GitHub

C++ Standard Template Library (STL) Tutorial

Complete Guide to C++23 STL

┌─────────────────────────────────────────────────────────────┐
│                    C++ STL ECOSYSTEM                        │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────┐   ┌─────────────┐   ┌─────────────┐        │
│  │ CONTAINERS  │   │  ITERATORS  │   │ ALGORITHMS  │        │
│  │             │◄──┤             │◄──┤             │        │
│  │  Data       │   │  Access     │   │  Process    │        │
│  │  Storage    │   │  Interface  │   │  Data       │        │
│  └─────────────┘   └─────────────┘   └─────────────┘        │
│        │                   │                  │             │
│        └───────────────────┴──────────────────┘             │
│                            │                                │
│                     ┌──────▼──────┐                         │
│                     │  ALLOCATORS │                         │
│                     │  & FUNCTORS │                         │
│                     └─────────────┘                         │
└─────────────────────────────────────────────────────────────┘

Overview

The C++ Standard Template Library (STL) is a powerful collection of template classes and functions that provide generic implementations of common data structures and algorithms. This tutorial covers everything from C++98 to the latest C++23 standard.

Tutorial Structure

Part 0: Foundations

0. Object-Oriented Programming Concepts

Part I: STL Fundamentals

1. Sequence Containers

2. Associative Containers

3. Unordered Containers

4. Container Adaptors

5. Iterators

6. Algorithms

7. C++20/23 Modern Features

8. Utility Containers

Part II: Advanced C++ Features

9. Templates

10. Lambdas and Functional Programming

11. Template Metaprogramming

12. Advanced Features

13. Best Practices and Idioms

Part III: Concurrent Programming

14. Multithreading and Concurrency

15. Asynchronous Programming and Futures

Part IV: I/O and System Programming

16. I/O, Filesystem, and Formatting

17. Exception Handling and Error Management

18. Time and Chrono Library

19. Memory Management and Allocators

20. Regular Expressions

21. Modules (C++20)

22. Coroutines (C++20)

Key Concepts

Time Complexity Notation

Memory Layout

┌─────────────────────────────────────────┐
│ Stack Memory (automatic storage)        │
│ - Small, fixed-size containers          │
│ - std::array                            │
└─────────────────────────────────────────┘

┌─────────────────────────────────────────┐
│ Heap Memory (dynamic storage)           │
│ - Dynamic containers                    │
│ - std::vector, std::map, etc.           │
│ - Managed via allocators                │
└─────────────────────────────────────────┘

Prerequisites

Compiler Support

How to Use This Tutorial

  1. Start with sequence containers if you’re new to STL
  2. Each file contains detailed explanations with ASCII diagrams
  3. Code examples are provided for each concept
  4. Practice exercises at the end of each section
  5. Build up to advanced topics like ranges and views

Quick Start Example

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    // Create a vector (dynamic array)
    std::vector<int> numbers = {5, 2, 8, 1, 9};
    
    // Sort using STL algorithm
    std::sort(numbers.begin(), numbers.end());
    
    // Print using range-based for loop (C++11)
    for (int num : numbers) {
        std::cout << num << " ";
    }
    // Output: 1 2 5 8 9
    
    return 0;
}

STL Philosophy

Generic Programming: Write code once, use with any type

Template Function/Class
        │
        ├─► Works with int
        ├─► Works with double
        ├─► Works with custom types
        └─► Works with any compatible type

Separation of Concerns:

Benefits of STL

  1. Reusability: Don’t reinvent the wheel
  2. Efficiency: Highly optimized implementations
  3. Type Safety: Compile-time type checking
  4. Flexibility: Works with any compatible type
  5. Standardization: Portable across platforms
  6. Modern C++: Constantly evolving with new standards

Learning Path

For Beginners

  1. Start with: OOP Concepts - Essential foundation
  2. Then learn: Sequence Containers
  3. Learn Algorithms
  4. Understand Iterators
  5. Explore Utility Containers

For Intermediate Developers

  1. Master Associative and Unordered Containers
  2. Study Modern C++20/23 Features
  3. Learn Lambdas
  4. Understand Templates

For Advanced Users

  1. Deep dive into Metaprogramming
  2. Master Advanced Features
  3. Study Best Practices
  4. Learn Multithreading
  5. Master Async Programming
  6. Learn Coroutines for elegant async code
  7. Explore I/O and Filesystem
  8. Study Exception Handling
  9. Understand Memory Management

Tutorial Contents Summary

Quick Reference (⭐ Bookmark this!)
└── Complete cheat sheet for daily C++ development

Part 0: Foundations (1 chapter)
└── Object-Oriented Programming fundamentals (classes, inheritance, polymorphism)

Part I: STL Fundamentals (8 chapters)
├── Containers (vectors, lists, maps, sets, etc.)
├── Algorithms (sorting, searching, transforming)
├── Iterators (accessing container elements)
└── Modern features (ranges, views, span)

Part II: Advanced C++ (5 chapters)
├── Templates (generic programming foundation)
├── Lambdas (functional programming)
├── Metaprogramming (compile-time computation)
├── Advanced features (move semantics, smart pointers)
└── Best practices (idioms, patterns, optimization)

Part III: Concurrent Programming (2 chapters)
├── Multithreading (threads, mutexes, atomics)
└── Async programming (futures, promises, parallel algorithms)

Part IV: I/O and System Programming (7 chapters)
├── I/O, filesystem, and formatting
├── Exception handling and error management
├── Time and chrono library
├── Memory management and allocators
├── Regular expressions
├── Modules (C++20)
└── Coroutines (C++20)

Total: 23 comprehensive chapters with examples and diagrams

🚀 Getting Started

📚 Quick Access


📖 Additional Resources

Quick Reference Guide

Condensed cheat sheet for daily development:

Bookmark this page for fast lookup while coding!


Complete tutorial: 23 comprehensive chapters + quick reference guide Covering C++98 through C++23 standard including OOP fundamentals