🔁 stack<T> – LIFO (Last-In-First-Out)
✅ Main Methods:
| Method | Description |
|---|---|
push(value) |
Add element on top |
pop() |
Remove top element |
top() |
Access top element |
empty() |
Check if stack is empty |
size() |
Number of elements |
✅ Key Features:
-
No iterators or random access
-
Built on
dequeby default -
Only access top
🔁 queue<T> – FIFO (First-In-First-Out)
✅ Main Methods:
| Method | Description |
|---|---|
push(value) |
Add element at back |
pop() |
Remove element from front |
front() |
Access first element |
back() |
Access last element |
empty() |
Check if empty |
size() |
Get number of elements |
✅ Key Features:
-
No iterators or random access
-
Built on
deque
🔁 map<Key, Value> – Ordered Map (Red-Black Tree)
✅ Main Methods:
| Method | Description |
|---|---|
insert({key, value}) |
Insert a key-value pair |
emplace(key, value) |
Efficient insert (constructs in-place) |
at(key) / operator[] |
Access value at key (creates if not exist) |
erase(key) |
Remove element by key |
find(key) |
Iterator to key or end() if not found |
begin(), end() |
Iterators (in key-sorted order) |
count(key) |
0 or 1 (only unique keys allowed) |
✅ Key Features:
-
Ordered by key (ascending by default)
-
O(log n) insert/find/delete
-
No duplicate keys allowed
🔁 unordered_map<Key, Value> – Hash Map
✅ Main Methods:
(Same as map, but implemented differently)
| Method | Description |
|---|---|
insert, emplace, at, operator[], erase, find |
|
begin(), end(), count(key) |
✅ Key Features:
-
Unordered (no guaranteed key order)
-
Hash table based (O(1) average time)
-
Faster than
mapfor large datasets (on average) -
No duplicate keys
| Container | Order | Key Access Time | Duplicate Keys | Usage |
|---|---|---|---|---|
stack<T> | N/A | N/A | N/A | LIFO operations |
queue<T> | N/A | N/A | N/A | FIFO operations |
map<K, V> | Sorted | O(log n) | ❌ No | When order matters |
unordered_map<K,V> | Unordered | O(1) average | ❌ No | When speed matters, not order |
🔹 vector<T>
✅ Features:
-
Dynamic array
-
Random access
-
O(1) for
push_back, amortized
📌 Main Methods:
v.push_back(x); // Add to end
v.pop_back(); // Remove last
v[i] or v.at(i); // Access element
v.size(); // Number of elements
v.clear(); // Remove all elements
v.begin(), v.end(); // Iterators
🔹 unordered_map<K, V>
✅ Features:
-
Hash table
-
O(1) average insert, erase, find
-
No ordering of keys
📌 Main Methods:
mp[key] = value; // Insert or update
mp.at(key); // Access with bounds check
mp.find(key); // Iterator to key
mp.erase(key); // Delete key
mp.count(key); // 0 or 1
🔹 map<K, V>
✅ Features:
-
Balanced BST (Red-Black Tree)
-
Ordered by key
-
O(log n) for insert, erase, find
📌 Main Methods:
(Same as unordered_map, but with order)
mp.lower_bound(key); // ≥ key
mp.upper_bound(key); // > key
🔹 set<T>
✅ Features:
-
Ordered unique elements
-
O(log n) insert/search
-
Sorted order
📌 Main Methods:
s.insert(x); // Add element
s.erase(x); // Remove element
s.count(x); // 0 or 1
s.find(x); // Iterator
🔹 unordered_set<T>
✅ Features:
-
Hash table for unique elements
-
O(1) average time
-
No order
📌 Main Methods:
(Same as set, but unordered)
🔹 priority_queue<T> (Max-Heap by default)
✅ Features:
-
Automatically keeps largest (or smallest) on top
-
Used in heaps, greedy, shortest path
📌 Main Methods:
pq.push(x); // Add element
pq.top(); // Max (or Min if min-heap)
pq.pop(); // Remove top
pq.empty(); // Check empty
📌 Min Heap:
priority_queue<int, vector<int>, greater<int>> minHeap;
🔹 deque<T>
✅ Features:
-
Double-ended queue
-
O(1) push/pop front and back
-
Used in sliding window problems
📌 Main Methods:
dq.push_back(x); // Add at back
dq.push_front(x); // Add at front
dq.pop_back();
dq.pop_front();
dq.front(), dq.back();
🔹 stack<T>
✅ Features:
-
LIFO (Last-In-First-Out)
📌 Main Methods:
s.push(x);
s.pop();
s.top();
s.empty();
🔹 queue<T>
✅ Features:
-
FIFO (First-In-First-Out)
📌 Main Methods:
q.push(x);
q.pop();
q.front(); q.back();
q.empty();
🔹 pair<T1, T2>
✅ Features:
-
Hold two related values
📌 Syntax:
pair<int, string> p = {1, "hello"};
p.first; p.second;
🔹 tuple<T1, T2, T3,...>
✅ Features:
-
Group multiple values
📌 Syntax:
tuple<int, int, string> t = {1, 2, "hi"};
get<0>(t); get<1>(t); get<2>(t);
🔹 bitset<N>
✅ Features:
-
Fixed-size bit representation
-
Great for bitmask DP
📌 Methods:
bitset<8> b("10101010");
b.count(); // Number of 1s
b.any(); b.none();
b.set(i); b.reset(i);
b[i]; // Access bit
No comments:
Post a Comment