Skip to content

1. Vector (vector<int> v)

MethodDescriptionTime Complexity
v.push_back()Adds an element to the endAmortized O(1)
v.pop_back()Removes the last elementO(1)
v.back()Accesses the last elementO(1)
v.front()Accesses the first elementO(1)
v.size()Returns the number of elementsO(1)
v.erase(it)Removes element at iterator itO(n)
v.erase(it1, it2)Removes elements in the range [it1, it2)O(n)
v.insert(it, val)Inserts val before the iterator itO(n)
v.clear()Removes all elementsO(n)
v.at(i)Accesses the element at index iO(1)
v[i]Accesses the element at index iO(1)

2. Map (map<int, int> mp)

MethodDescriptionTime Complexity
mp.insert({key, val})Inserts a key-value pairO(log n)
mp.erase(key)Removes an element by keyO(log n)
mp.erase(it)Removes element at iterator itO(1)
mp.find(key)Finds the iterator to the key, if it existsO(log n)
mp.count(key)Checks if the key exists (returns 1 or 0)O(log n)
mp[key]Accesses or inserts the value associated with keyO(log n)
mp.clear()Removes all elementsO(n)
mp.size()Returns the number of elementsO(1)
cpp
 // Access key and value from map iterator
auto it=mp.begin() ;
int first=it->first  ,second=it->second ;

Operationmap (Balanced Tree)unordered_map (Hash Table)
InsertionO(log n)O(1) average, O(n) worst case
DeletionO(log n)O(1) average, O(n) worst case
SearchO(log n)O(1) average, O(n) worst case
Ordered traversalYes (in key order)No (unordered traversal)

3. Set (set<int> s)

MethodDescriptionTime Complexity
s.insert(val)Inserts an elementO(log n)
s.erase(it)Removes element at iterator itO(1)
s.erase(val)Removes the element with value valO(log n)
s.find(val)Finds the iterator to the element, if it existsO(log n)
s.count(val)Checks if the value existsO(log n)
s.clear()Removes all elementsO(n)
s.size()Returns the number of elementsO(1)
s.begin()Returns an iterator to the first elementO(1)
s.end()Returns an iterator to the element after the lastO(1)
cpp
auto it=s.begin();
int num=*it ; // Dereference set iterator to get value

Operationset (Balanced Tree)unordered_set (Hash Table)
InsertionO(log n)O(1) average, O(n) worst case
DeletionO(log n)O(1) average, O(n) worst case
SearchO(log n)O(1) average, O(n) worst case
Ordered traversalYes (in key order)No (unordered traversal)
Access by keyO(log n)O(1) average, O(n) worst case
SizeO(1)O(1)
ClearO(n)O(n)

4. Queue (queue<int> q)

MethodDescriptionTime Complexity
q.push(val)Adds an element to the backO(1)
q.pop()Removes the front elementO(1)
q.front()Accesses the front elementO(1)
q.back()Accesses the back elementO(1)
q.size()Returns the number of elementsO(1)
q.empty()Checks if the queue is emptyO(1)

5. Priority Queue (priority_queue<int> pq)

MethodDescriptionTime Complexity
pq.push(val)Adds an element (maintains heap property)O(log n)
pq.pop()Removes the top element (maintains heap property)O(log n)
pq.top()Accesses the top elementO(1)
pq.size()Returns the number of elementsO(1)
pq.empty()Checks if the priority queue is emptyO(1)

6. Stack (stack<int> st)

MethodDescriptionTime Complexity
st.push(val)Pushes an element onto the stackO(1)
st.pop()Removes the top elementO(1)
st.top()Accesses the top elementO(1)
st.size()Returns the number of elementsO(1)
st.empty()Checks if the stack is emptyO(1)

<<>> with ♥️ by S@Nchit