Unit 5: Standard Template Library (STL) & Advanced ConceptsLesson #16 / 20

Lesson 16: Master C++ STL Containers: vector, list, map & unordered_map

NSNextSemβ€’Last Updated: 5 Aug, 2026

πŸ—ΊοΈ The Standard Template Library (STL)

The C++ STL provides powerful, production-grade template data structures and algorithms.


πŸ“¦ Key STL Containers Overview

  • ⚑ std::vector<T>: Contiguous dynamic array with fast random access (O(1)).
  • πŸ”— std::list<T>: Doubly linked list with fast insertion/deletion anywhere (O(1)).
  • 🌲 std::map<Key, Value>: Self-balancing Red-Black Tree keeping keys sorted (O(log N)).
  • ⚑ std::unordered_map<K, V>: Hash table providing constant time lookup (O(1)).
  • πŸ›‘οΈ std::set<T>: Collection of unique sorted elements.

  • πŸ’» Code Example: Key-Value Hash Map & Vector Operations

    ⊞C++ Source Code (main.cpp)
    #include <iostream>
    #include <unordered_map>
    #include <string>
    int main() {
    std::unordered_map<std::string, double> productPrices;
    productPrices["Intel Core i9"] = 589.99;
    productPrices["NVIDIA RTX 4090"] = 1599.99;
    productPrices["DDR5 32GB RAM"] = 129.99;
    std::cout << "--- PC Component Prices (Hash Table Lookup) ---" << std::endl;
    for (const auto &[item, price] : productPrices) {
    std::cout << "πŸ“¦ " << item << " => $" << price << std::endl;
    }
    return 0;
    }
    ⊞Windows 11 Pro Terminal Execution OutputWindows 11 Pro Verified
    ⊞Administrator: Windows 11 Pro Command Prompt (g++ GCC 13.2.0)
    β€”β–‘βœ•

    Microsoft Windows [Version 10.0.22631.3007] (Windows 11 Pro x86_64)

    (c) Microsoft Corporation. All rights reserved.

    C:\Users\Student\CPP_Project>#include <iostream>

    C:\Users\Student\CPP_Project> g++ -o main.exe main.cpp C:\Users\Student\CPP_Project> main.exe Hello World from C++ on Windows 11 Pro! [Process exited with code 0 in 0.001 seconds]

    Interactive Knowledge Check

    Test your understanding of Lesson #16 concepts

    Which STL container stores key-value pairs in O(1) average hash lookup time?