</>

🗺️ std::map Quick Review 🚀


What it is: An associative container storing key-value pairs, where keys are always kept in sorted order. It's typically implemented as a balanced binary search tree (like a Red-Black Tree).

Core Benefit: Guaranteed O(log N) time complexity for most operations and automatic key sorting.


✨ Essentials & Syntax

Operation Code Example Notes
Include #include <map>
Declaration std::map<KeyType, ValueType> myMap; KeyType must be LessThanComparable (<).
Insert/Update myMap["apple"] = 10; Adds if new, updates if key exists.
Access (Safe) int val = myMap.at("apple"); Throws std::out_of_range if key not found.
Access (Unsafe) int val = myMap["grape"]; WARNING: Inserts default if key not found.
Check Exist. if (myMap.count("apple")) { ... } Returns 1 if key exists, 0 otherwise.
Find (Iterator) auto it = myMap.find("banana"); Returns iterator or myMap.end().
Delete myMap.erase("apple"); Removes key-value pair.
Size myMap.size(); Number of elements.
Is Empty? myMap.empty(); Returns true if no elements.

🚶 Traversal Ways (Always Sorted!)


💡 Key Use Cases


🧠 Key Differences from unordered_map

Feature std::map std::unordered_map
Order Sorted by key No specific order
Structure Balanced Binary Search Tree Hash Table
Complexity O(log N) (guaranteed) Avg O(1), Worst O(N)
Key Req. LessThanComparable (<) Hashable & Equality Comparable (==)