Skip to content

Part 4: STL Containers Intermediate

Làm chủ C++ Standard Library — Containers là nền tảng của mọi chương trình C++ hiện đại

Tại sao học STL?

STL (Standard Template Library) là bộ công cụ sẵn có trong mọi C++ compiler. Thay vì tự viết linked list, hash table, hay sorting algorithm, bạn sử dụng những implementations đã được tối ưu qua hàng chục năm.

📌 HPN Standard

Rule: Luôn ưu tiên STL containers/algorithms trước khi tự viết. "Don't reinvent the wheel."


Module Structure

📘 Part 1: Core Containers

TopicDescription
std::vectorDynamic array — container phổ biến nhất
std::stringText handling với string operations
std::map & unordered_mapKey-value storage
Performance AnalysisBig O của từng container
IteratorsTraversing containers
Modern ArraysStop using C-arrays!

📙 Part 2: Algorithms & Lambdas

TopicDescription
Algorithms Overview<algorithm><numeric> headers
Lambda ExpressionsInline functions và closures
Sorting & ComparatorsCustom sort rules
Searchingfind, binary_search, bounds
Transform & ReduceFunctional operations
⚡ Code GolfSTL power demonstrations

The Big 3 Containers

┌─────────────────────────────────────────────────────────────────┐
│                    THE BIG 3 CONTAINERS                         │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌─────────────────┐                                            │
│  │   std::vector   │  Dynamic array                             │
│  │   ─────────────  │  • Contiguous memory                      │
│  │   [1][2][3][4]  │  • Fast random access O(1)                 │
│  │                 │  • Cache-friendly                          │
│  └─────────────────┘  • Use 90% of the time                     │
│                                                                 │
│  ┌─────────────────┐                                            │
│  │   std::string   │  Text container                            │
│  │   ─────────────  │  • UTF-8 aware operations                 │
│  │   "Hello"       │  • Concatenation, search, replace          │
│  │                 │  • Small String Optimization (SSO)         │
│  └─────────────────┘                                            │
│                                                                 │
│  ┌─────────────────┐                                            │
│  │   std::map      │  Sorted key-value                          │
│  │   ─────────────  │  • O(log n) lookup                        │
│  │   {a:1, b:2}    │  • Use unordered_map for O(1)              │
│  │                 │  • Iteration in sorted order               │
│  └─────────────────┘                                            │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Prerequisites

📋 YÊU CẦU


Learning Path

  1. 📦 std::vector — Container phổ biến nhất
  2. 📝 std::string — Text manipulation
  3. 🗺️ std::map — Key-value pairs
  4. Performance — Big O analysis
  5. 🔄 Iterators — Traversal abstraction
  6. 🚫 Modern Arrays — Goodbye C-arrays!

"Use std::vector by default. Use other containers only if you have a reason." — Bjarne Stroustrup