When should I use vector vs list?
vector default for sequential data—cache friendly; list for frequent middle inserts (rare in practice).
Master C++ Standard Template Library (STL): containers, algorithms, iterators, functors. Learn with practical examples and best practices for efficient programming.
Data Structures
Sort, Search, Transform
Container Access
Function Objects
The STL provides battle-tested containers and algorithms. Learn vectors, maps, iterators, and common algorithms—productivity multipliers for interviews and production code.
STL questions appear in most C++ interviews—complexity, iterator invalidation, and when to pick vector vs map.
The Standard Template Library (STL) is a powerful set of C++ template classes that provide common data structures and algorithms. STL is a core part of C++ Standard Library and revolutionized C++ programming with generic programming concepts.
Algorithms use iterators to operate on containers
The following table compares different STL containers with their characteristics, time complexities, and use cases:
| Container | Header | Characteristics | Time Complexity | Use Cases |
|---|---|---|---|---|
| vector | #include <vector> | Dynamic array, random access | Access: O(1) Insert/Delete: O(n) | Default choice, when need array-like |
| deque | #include <deque> | Double-ended queue, fast ends | Push/Pop ends: O(1) Middle ops: O(n) | Queue/stack, sliding window |
| list | #include <list> | Doubly linked list | Insert/Delete: O(1) Access: O(n) | Frequent insertions/deletions |
| forward_list | #include <forward_list> | Singly linked list | Insert/Delete: O(1) Access: O(n) | Memory efficient list |
| array | #include <array> | Fixed-size array (C++11) | All ops: O(1) | Fixed size, stack allocation |
| set | #include <set> | Unique keys, sorted | Most ops: O(log n) | Unique collection, sorted |
| multiset | #include <set> | Duplicate keys, sorted | Most ops: O(log n) | Sorted bag, with duplicates |
| map | #include <map> | Key-value pairs, sorted keys | Most ops: O(log n) | Dictionary, associative array |
| multimap | #include <map> | Multiple values per key, sorted | Most ops: O(log n) | One-to-many mapping |
| unordered_set | #include <unordered_set> | Unique keys, hashed | Avg: O(1) Worst: O(n) | Fast lookup, no ordering |
| unordered_map | #include <unordered_map> | Key-value pairs, hashed | Avg: O(1) Worst: O(n) | Fast dictionary, no ordering |
| stack | #include <stack> | LIFO (uses deque by default) | All ops: O(1) | Function calls, undo/redo |
| queue | #include <queue> | FIFO (uses deque by default) | All ops: O(1) | BFS, task scheduling |
| priority_queue | #include <queue> | Max-heap (uses vector by default) | Push/Pop: O(log n) Top: O(1) | Dijkstra, scheduling |
Sequence containers store elements in a linear sequence. They maintain the order of insertion and allow access by position.
vector<int> v = {1, 2, 3};
v.push_back(4);
v.pop_back();
list<int> l = {1, 2, 3};
l.push_front(0);
l.pop_front();
deque<int> d = {1, 2, 3};
d.push_front(0);
d.push_back(4);
vector v = {1,2,3};
v.push_back(4); list a={1,2}, b={3};
a.splice(a.end(), b); deque d;
d.push_front(1);
d.push_back(2); array a = {1,2,3};
cout << a[1]; for (int x : v) cout << x;v.reserve(1000); // fewer reallocationsAssociative containers store elements in sorted order (or hashed) for fast lookup based on keys. They implement tree or hash table data structures.
set<int> s = {3, 1, 2};
s.insert(4);
s.find(2);
map<string, int> m;
m["Alice"] = 25;
m.find("Alice");
unordered_set<int> us;
us.insert(10);
us.find(10);
unordered_map<string, int> um;
um["Bob"] = 30;
um.find("Bob");
map m;
m["apple"] = 3; unordered_map um;
um["kiwi"] = 5; set s = {3,1,3,2};
// stores 1,2,3 multimap mm;
mm.insert({'a',1});
mm.insert({'a',2}); auto it = m.find("apple");
if (it != m.end()) cout << it->second;set st={1,3,5};
auto it = st.lower_bound(3); Use ordered containers (set/map) when you need sorted elements or range queries. Use unordered containers when you need faster lookups and don't care about order.
Container adaptors provide specialized interfaces for stacks, queues, and priority queues. They use underlying containers but restrict access patterns.
stack<int> s;
s.push(10);
s.top();
s.pop();
queue<int> q;
q.push(10);
q.front();
q.pop();
priority_queue<int> pq;
pq.push(10);
pq.top();
pq.pop();
stack st;
st.push(10);
cout << st.top(); st.pop(); queue q;
q.push(1); q.push(2);
cout << q.front(); q.pop(); priority_queue pq;
pq.push(3); pq.push(10);
cout << pq.top(); stack> custom; if (!q.empty()) q.pop();cout << st.size();STL provides over 100 algorithms that operate on ranges of elements. Algorithms are template functions that work with iterators.
find(), count()
for_each(), all_of()
search(), mismatch()
copy(), transform()
replace(), fill()
generate(), remove()
sort(), stable_sort()
partial_sort()
nth_element()
accumulate()
inner_product()
partial_sum()
adjacent_difference()
vector v={5,1,4};
sort(v.begin(), v.end()); auto it = find(v.begin(), v.end(), 4);int c = count_if(v.begin(), v.end(),
[](int x){ return x%2==0; });int sum = accumulate(v.begin(), v.end(), 0);transform(v.begin(), v.end(), v.begin(),
[](int x){ return x*2; });auto it = max_element(v.begin(), v.end());back_inserter, front_inserter, inserter for outputstd::begin() and std::end() for C-style arraysIterators provide a uniform way to access container elements. Functors (function objects) allow treating functions as objects.
for (auto it=v.begin(); it!=v.end(); ++it)
cout << *it;for (auto it=v.rbegin(); it!=v.rend(); ++it)
cout << *it;back_insert_iterator> bi(v);
*bi++ = 9; sort(v.begin(), v.end(), less()); remove_if(v.begin(), v.end(), [](int x){return x<0;});cout << distance(v.begin(), v.end());auto for iterator types (C++11)emplace operations when possible (C++11)unordered_map over map when order not neededempty() before front()/back()for (const auto& x : v) cout << x;v.reserve(n);sort(v.begin(), v.end());v.emplace_back(42);for (int i=0; i<=v.size(); ++i) {}v.push_back(1); // it may be invalidvector default for sequential data—cache friendly; list for frequent middle inserts (rare in practice).
Certain operations (resize, erase) invalidate iterators—do not use stale iterators afterward.
std::map is ordered tree; std::unordered_map is hash table with average O(1) lookup.