πmap in C++


πΉ What is map
?
A container in C++ (from STL) that stores key β value pairs.
Each key is unique.
Keys are stored in sorted order (ascending by default).
Internally implemented as a balanced binary search tree (Red-Black Tree).
π Think of it as a dictionary: word (key) β meaning (value).
πΉ Syntax
map<KeyType, ValueType> mp;
Example:
map<string, int> age;
age["Alice"] = 20;
age["Bob"] = 25;
Stored as:
"Alice" β 20
"Bob" β 25
πΉ Key vs Value
Key = used for searching/ordering.
Value = information linked to that key.
In
map<string,int>
,string
= key,int
= value.
πΉ Common Operations & Complexity
Operation | Code Example | Time Complexity |
Insert | mp[key] = value; or mp.insert({key,value}); | O(log n) |
Access (by key) | mp[key] | O(log n) |
Search | mp.find(key) | O(log n) |
Erase | mp.erase(key) | O(log n) |
Size | mp.size() | O(1) |
Iterate | for (auto &p: mp) ... | O(n) |
πΉ Example
map<int, string> mp;
mp[30] = "X";
mp[10] = "Y";
mp[20] = "Z";
// Stored as sorted by key:
10 β "Y"
20 β "Z"
30 β "X"
πΉ Checking Existence
if (mp.find(20) != mp.end()) {
cout << "Key 20 found, value = " << mp[20];
}
else {
cout << "Key 20 not found";
}
πΉ Iteration
for (auto it = mp.begin(); it != mp.end(); it++) {
cout << it->first << " β " << it->second << endl;
}
// or
for (auto &p : mp) {
cout << p.first << " β " << p.second << endl;
}
πΉ Characteristics
Sorted order
Integers sorted numerically.
Strings sorted lexicographically.
Custom keys sorted using comparator.
Unique keys only
- If you insert the same key again, it updates the value.
Underlying data structure
Red-Black Tree (balanced BST).
This is why all operations are O(log n).
πΉ Advantages (Pros)
β
Keeps keys sorted automatically
β
Fast lookup, insert, erase β O(log n)
β
Works with any data type as key (numbers, strings, pairs, custom structs with comparator)
πΉ Disadvantages (Cons)
β Slower than unordered_map
(O(1) average)
β More memory overhead (tree structure)
β If you donβt need sorting, itβs wasteful
πΉ map
vs unordered_map
Feature | map | unordered_map |
Order of keys | Sorted | No order |
Complexity | O(log n) | O(1) avg, O(n) worst |
Use case | Need order / ranges | Just fast lookups |
Internal structure | Red-Black Tree | Hash Table |
πΉ Typical LeetCode Use Cases
Counting frequency
map<int,int> freq; for (int x : nums) freq[x]++;
Checking existence
if (mp.find(target) != mp.end()) ...
Two Sum problem (using number β index mapping).
Tracking smallest/largest key (since ordered).
β Summary
map
= ordered key-value container (Red-Black Tree).Operations = O(log n).
Keys always sorted.
Use when order matters or you need range queries.
Prefer
unordered_map
for faster average lookups when order doesnβt matter.
Subscribe to my newsletter
Read articles from Abhinav Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abhinav Kumar
Abhinav Kumar
π B.Tech CSE | π¨βπ» Learning DSA & C++ | π Building projects & writing what I learn | π Currently solving LeetCode & exploring Git/GitHub