C++ Cheatsheet
48 essential methods for coding interviews
Showing 48 of 48 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
push_back | vec.push_back(value) | Adds element to the end of vector | O(1) amortized | essential |
pop_back | vec.pop_back() | Removes the last element from vector | O(1) | essential |
size | vec.size() | Returns number of elements in container | O(1) | essential |
empty | vec.empty() | Returns true if container has no elements | O(1) | essential |
front/back | vec.front() / vec.back() | Access first/last element of vector | O(1) | essential |
erase | vec.erase(iterator) / vec.erase(first, last) | Removes element(s) at position or range | O(n) | essential |
substr | str.substr(pos, len) | Returns substring starting at pos with length len | O(len) | essential |
find | str.find(substr, pos) | Finds first occurrence of substring starting from pos | O(n*m) | essential |
append/+= | str.append(s) / str += s | Appends string or character to end | O(m) where m is appended length | essential |
stoi/stol/stod | stoi(str) / stol(str) / stod(str) | Converts string to int/long/double | O(n) | essential |
to_string | to_string(num) | Converts numeric value to string | O(log n) | essential |
map insert/[] | map[key] = value / map.insert({key, value}) | Inserts or updates key-value pair in ordered map | O(log n) | essential |
map find | map.find(key) | Returns iterator to element or end() if not found | O(log n) | essential |
map count | map.count(key) | Returns 1 if key exists, 0 otherwise | O(log n) | essential |
unordered_map insert/[] | umap[key] = value | Inserts or updates key-value pair in hash map | O(1) average, O(n) worst | essential |
unordered_map find | umap.find(key) | Returns iterator to element or end() if not found | O(1) average, O(n) worst | essential |
set insert | set.insert(value) | Inserts element into ordered set | O(log n) | essential |
set find | set.find(value) | Returns iterator to element or end() if not found | O(log n) | essential |
set count | set.count(value) | Returns 1 if element exists, 0 otherwise | O(log n) | essential |
unordered_set insert | uset.insert(value) | Inserts element into hash set | O(1) average, O(n) worst | essential |
unordered_set count | uset.count(value) | Returns 1 if element exists, 0 otherwise | O(1) average | essential |
sort | sort(begin, end) / sort(begin, end, comparator) | Sorts elements in ascending order (or by custom comparator) | O(n log n) | essential |
binary_search | binary_search(begin, end, value) | Returns true if value exists in sorted range | O(log n) | essential |
lower_bound | lower_bound(begin, end, value) | Returns iterator to first element >= value | O(log n) | essential |
upper_bound | upper_bound(begin, end, value) | Returns iterator to first element > value | O(log n) | essential |
stack push/pop/top | stk.push(val) / stk.pop() / stk.top() | LIFO operations: add, remove, and access top element | O(1) | essential |
queue push/pop/front | q.push(val) / q.pop() / q.front() | FIFO operations: add to back, remove from front | O(1) | essential |
priority_queue (max-heap) | priority_queue<int> pq; | Max-heap: largest element always on top | O(log n) push/pop, O(1) top | essential |
priority_queue (min-heap) | priority_queue<int, vector<int>, greater<int>> pq; | Min-heap: smallest element always on top | O(log n) push/pop, O(1) top | essential |
for-each loop | for (auto& x : container) | Range-based for loop over container elements | O(n) | essential |
reverse | reverse(begin, end) | Reverses elements in range | O(n) | essential |
swap | swap(a, b) | Swaps values of two variables | O(1) | essential |
insert | vec.insert(iterator, value) | Inserts element at specified position | O(n) | common |
reserve | vec.reserve(n) | Pre-allocates memory for n elements | O(n) | common |
resize | vec.resize(n) / vec.resize(n, value) | Changes container size, adding/removing elements as needed | O(n) | common |
clear | vec.clear() | Removes all elements from container | O(n) | common |
c_str | str.c_str() | Returns null-terminated C-style string pointer | O(1) | common |
compare | str1.compare(str2) | Compares strings lexicographically, returns <0, 0, or >0 | O(n) | common |
map erase | map.erase(key) / map.erase(iterator) | Removes element by key or iterator | O(log n) | common |
set erase | set.erase(value) | Removes element from set | O(log n) | common |
stable_sort | stable_sort(begin, end) | Sorts preserving relative order of equal elements | O(n log n) | common |
nth_element | nth_element(begin, nth, end) | Partitions so nth element is in sorted position | O(n) average | common |
find (algorithm) | find(begin, end, value) | Linear search for value, returns iterator | O(n) | common |
max_element/min_element | max_element(begin, end) / min_element(begin, end) | Returns iterator to max/min element | O(n) | common |
accumulate | accumulate(begin, end, init) | Computes sum (or fold) of range | O(n) | common |
make_pair | make_pair(first, second) | Creates a pair object | O(1) | common |
fill | fill(begin, end, value) | Fills range with specified value | O(n) | common |
partial_sort | partial_sort(begin, middle, end) | Sorts first (middle-begin) elements | O(n log k) | useful |