Notation: n = number of elements in container. Complexities are typical/average unless noted.
v[i]O(1) (unchecked)v.at(i)O(1) (checked; may throw)v.front(),v.back()O(1)v.size(),v.empty()O(1)
v.push_back(x)amortized O(1)v.pop_back()O(1)v.insert(pos, x)O(n) (shifts elements)v.erase(pos)/v.erase(l,r)O(n)v.clear()O(n) (destroys elements)
find(v.begin(), v.end(), x)O(n)binary_search(v.begin(), v.end(), x)O(log n) (requires sorted)lower_bound/upper_boundO(log n) (requires sorted)
for (auto &e : v) {}O(n)for (auto it=v.begin(); it!=v.end(); ++it) {}O(n)
s[i],s.at(i)O(1)s.size()/s.length()O(1) (same)s.front(),s.back()O(1)
s.push_back(c)amortized O(1)s.pop_back()O(1)s += t/s.append(t)O(|t|)s.substr(pos, len)O(len)s.clear()O(n)
s.find(x)O(n) typicals.find(x, pos)O(n) typical
for (char c : s) {}O(n)
a[i],a.at(i)O(1)a.size()O(1)
- None (size fixed)
a.fill(val)O(N)
for (auto &x : a)O(N)
Tip: Use array<int,26> cnt{}; for counting letters.
mp[key]avg O(1)
Creates key with default value if missing.mp.at(key)avg O(1) (throws if missing)mp.size(),mp.empty()O(1)
mp[key] = valavg O(1)mp.insert({k,v})avg O(1) (won’t overwrite existing key)mp.erase(key)avg O(1)mp.clear()O(n)
mp.count(key)avg O(1) (0 or 1)mp.find(key)avg O(1) (iterator ormp.end())
for (auto &p : mp) { p.first, p.second }O(n) (order not guaranteed)
Same API as unordered_map, but:
- ordered by key
- operations O(log n)
mp[key]O(log n) (inserts default if missing)mp.at(key)O(log n)
mp.insert(...)/mp.erase(...)O(log n)
mp.find(key)O(log n)mp.lower_bound(key)O(log n) (first key >= key)mp.upper_bound(key)O(log n) (first key > key)
- in increasing key order:
for (auto &p : mp)O(n)
st.count(x)avg O(1) (0/1)st.find(x)avg O(1)
st.insert(x)avg O(1) returnspair<it,bool>st.erase(x)avg O(1)st.clear()O(n)
for (auto &x : st)O(n) (order not guaranteed)
Pro tip (duplicate detection):
if (!st.insert(x).second) { /* duplicate */ }- insert returns:
pair<iterator, bool>.second == true if insertion happened (value was not present), false if value already existed
All ops are O(log n), elements are sorted.
insert,erase,find,countO(log n)lower_bound/upper_boundO(log n) Iteration is sorted order O(n).
q.front(),q.back()O(1)q.size(),q.empty()O(1)
q.push(x)O(1)q.pop()O(1)
Iteration: not supported directly (you pop to traverse).
st.top()O(1)st.push(x)O(1)st.pop()O(1)st.size(),st.empty()O(1)
push_back,push_front,pop_back,pop_frontO(1)front,back,size,emptyO(1) Used for sliding window / monotonic queue patterns.
(Default: max-heap)
pq.push(x)O(log n)pq.pop()O(log n)pq.top()O(1)pq.size(),pq.empty()O(1)
Min-heap:
priority_queue<int, vector<int>, greater<int>> pq;sort(v.begin(), v.end())O(n log n)reverse(v.begin(), v.end())O(n)max_element(...),min_element(...)O(n)accumulate(..., 0LL)O(n)count(...)O(n)find(...)O(n)