C++ STL Cheatsheet - Version 1.0.
0
Essentials for Competitive Programming and Technical Interviews
By Habibur Rahman | ML Developer, Ex-Software Engineer
February 4, 2024
Containers Algorithms
Vector (vector) Sort
• Operations: Access (O(1)), Insert/Delete at end (O(1) amortized), Insert/Delete • Time Complexity: O(n log n).
elsewhere (O(n)). • Note: By default, it sorts in ascending order, algorithm used is IntroSort.
• Note: vector is implemented as a dynamic array.
#include <algorithm>
#include <vector> std::sort(vec.begin(), vec.end()); // Ascending
std::vector<int> vec; // Declaration std::sort(vec.begin(), vec.end(), std::greater<int>()); // Descending
vec.push_back(10); // Insert at the end - Amortized `O(1)`
vec.size(); // Get the number of elements - `O(1)` Binary Search (binary_search)
vec.insert(vec.begin() + i, 20); // Insert at index i (0-based) - `O(n)` • Time Complexity: O(log n).
vec.erase(vec.begin() + i); // Remove at index i (0-based) - `O(n)` • Note: The array must be sorted.
vec.pop_back(); // Remove the last element - `O(1)`
std::binary_search(vec.begin(), vec.end(), val);
vec.clear(); // Remove all elements - `O(n)`
Set (set) Lower Bound and Upper Bound
• Operations: Insert/Delete/Search (O(log n)). • Time Complexity: O(log n).
• Note: set is implemented as a balanced binary search tree (Red-Black Tree). • Note: The array must be sorted.
#include <set> auto lb = std::lower_bound(vec.begin(), vec.end(), val); // Not less than val
std::set<int> s; auto ub = std::upper_bound(vec.begin(), vec.end(), val); // Greater than val
s.insert(1); // Insert an element - `O(log n)`
s.find(1); // Find an element - `O(log n)`
Max Element and Min Element
s.erase(1); // Remove an element - `O(log n)` • Time Complexity: O(n).
s.clear(); // Remove all elements - `O(n)` • Note: Returns an iterator to the maximum/minimum element.
s.size(); // Get the number of elements - `O(1)` auto max_it = std::max_element(vec.begin(), vec.end());
Map (map) auto min_it = std::min_element(vec.begin(), vec.end());
• Operations: Insert/Delete/Search by key (O(log n)).
• Note: map is implemented as a balanced binary search tree (Red-Black Tree). Utilities
Pair (pair)
#include <map>
std::map<int, int> m; • Simple container to store two values.
m[1] = 2; // Insert or update a key-value pair - `O(log n)` #include <utility>
m.emplace(1, 2); // Insert a key-value pair - `O(log n)` std::pair<int, int> p = {1, 2};
m.find(1); // Find an element by key - `O(log n)`
m.erase(1); // Remove an element by key - `O(log n)`
Tuple (tuple)
m.clear(); // Remove all elements - `O(n)` • Generalization of pair to hold more than two items.
m.size(); // Get the number of elements - `O(1)` #include <tuple>
Unordered Set (unordered_set) std::tuple<int, char, double> t = {1, 'a', 2.0};
• Operations: Insert/Delete/Search (Average: O(1), Worst: O(n)). Swap
• Note: unordered_set is implemented as a hash table.
• Exchange the values of two variables. O(1).
#include <unordered_set>
std::swap(a, b);
std::unordered_set<int> us;
us.insert(1); // Insert an element - `Average: O(1)`, Worst: `O(n)` Reverse
us.find(1); // Find an element - `Average: O(1)`, Worst: `O(n)`
us.erase(1); // Remove an element - `Average: O(1)`, Worst: `O(n)` • Reverse the elements of a container. O(n).
us.clear(); // Remove all elements - `O(n)` std::reverse(vec.begin(), vec.end());
us.size(); // Get the number of elements - `O(1)`
Unordered Map (unordered_map) Next Permutation and Previous Permutation
• Rearrange the elements into the next/previous lexicographically greater permu-
• Operations: Insert/Delete/Search by key (Average: O(1), Worst: O(n)).
tation. O(n).
• Note: unordered_map is implemented as a hash table.
std::next_permutation(vec.begin(), vec.end());
#include <unordered_map>
std::prev_permutation(vec.begin(), vec.end());
std::unordered_map<int, int> um;
um[1] = 2; // Insert or update a key-value pair-`Average: O(1)`, Worst: `O(n)` Fill
um.emplace(1, 2); // Insert a key-value pair - `Average: O(1)`, Worst: `O(n)`
• Assigns the given value to the elements in the range. O(n).
um.find(1); // Find an element by key - `Average: O(1)`, Worst: `O(n)`
um.erase(1); // Remove an element by key - `Average: O(1)`, Worst: `O(n)` std::fill(vec.begin(), vec.end(), val);
um.clear(); // Remove all elements - `O(n)`
um.size(); // Get the number of elements - `O(1)` Memory Set
Stack (stack) • Fills the first n bytes of the memory area pointed to by ptr with the constant
byte val. O(n).
• Operations: Push/Pop/Top (O(1)).
• Note: stack is implemented as a deque (double-ended queue). #include <cstring>
std::memset(ptr, val, n); // Entire with val std::memset(ptr, -1, sizeof(ptr));
#include <stack>
std::stack<int> st; Min and Max
st.push(10); // Push an element - `O(1)` • Returns the minimum/maximum of two values.
st.pop(); // Pop an element - `O(1)`
st.top(); // Get the top element - `O(1)` std::min(a, b);
st.size(); // Get the number of elements - `O(1)` std::max(a, b);
st.empty(); // Check if the stack is empty - `O(1)`
st.clear(); // Remove all elements - `O(n)` Absolute
• Returns the absolute value of a number.
Queue (queue)
• Operations: Enqueue/Dequeue/Front (O(1)). std::abs(val);
• Note: queue is implemented as a deque (double-ended queue). String Stream
#include <queue> • Used to manipulate strings as if they were input/output streams.
std::queue<int> q;
q.push(10); // Enqueue an element - `O(1)` #include <sstream>
q.pop(); // Dequeue an element - `O(1)` std::stringstream ss;
q.front(); // Get the front element - `O(1)` ss<<"Hello";
q.size(); // Get the number of elements - `O(1)` ss>>str;
q.empty(); // Check if the queue is empty - `O(1)`
q.clear(); // Remove all elements - `O(n)`
String Functions
• stoi, stol, stoll, stoul, stoull, stof, stod, stold: Convert string to inte-
Priority Queue (priority_queue) ger/long/long long/unsigned long/unsigned long long/float/double/long double.
• Operations: Insert/Delete Max (O(log n)), Get Max (O(1)). • to_string: Convert number to string.
• Note: priority_queue is implemented as a heap. • getline: Read a line from input stream.
#include <queue> std::stoi("10");
std::priority_queue<int> pq; std::to_string(10);
pq.push(10); // Insert an element - `O(log n)` std::getline(std::cin, str);
pq.pop(); // Remove the top element - `O(log n)`
pq.top(); // Get the top element - `O(1)` Count
pq.size(); // Get the number of elements - `O(1)` • Count the number of occurrences of a value in a range.
pq.empty(); // Check if the priority queue is empty - `O(1)`
pq.clear(); // Remove all elements - `O(n)` std::count(vec.begin(), vec.end(), val);