Think of set and map as C++ built in implementation of Binary Search Tree, where every operation can be used in $O(log n)$ time. The only difference is that map stores a key-value pair and set stores only the key.
Set
Operation
Time Complexity
Explanation
s.insert(x)
O(log n)
Insert x into the set (maintains order).
s.erase(x)
O(log n)
Removes x from the set.
s.find(x)
O(log n)
Returns an iterator to x, or s.end() if not found.
s.count(x)
O(log n)
Checks if x exists in the set (returns 0 or 1).
s.lower_bound(x)
O(log n)
Finds the first element ≥ x.
s.upper_bound(x)
O(log n)
Finds the first element > x.
s.begin() / s.end()
O(1)
Returns an iterator to the first/last element.
s.size()
O(1)
Returns the number of elements in the set.
Map
Operation
Time Complexity
Explanation
m[key] = value;
O(log n)
Insert or update key → value.
m.insert({key, value})
O(log n)
Insert a new key-value pair.
m.erase(key)
O(log n)
Removes the key from the map.
m.find(key)
O(log n)
Returns an iterator to the key, or m.end() if not found.
m.count(key)
O(log n)
Checks if key exists (returns 0 or 1).
m.lower_bound(key)
O(log n)
Finds the first key ≥ key.
m.upper_bound(key)
O(log n)
Finds the first key > key.
m.begin() / m.end()
O(1)
Returns an iterator to the first/last key-value pair.
m.size()
O(1)
Returns the number of key-value pairs.
Heap
Here is a good video on heap. A heap is a complete binary tree where the root node is alawys larger than its children.