
STL in C++
Introduction
The Standard Template Library (STL) in C++ is a collection of class templates and functions that provide powerful, ready-to-use data structures and algorithms. STL allows you to write clean, efficient, and reusable code.
Quick Tip: #include <bits/stdc++.h>
includes almost everything in STL. Use it for competitive programming only. Otherwise, include individual headers for clean code.
STL Components
STL is broadly divided into:
-
Containers – Store data
-
Algorithms – Manipulate data
-
Iterators – Traverse containers
-
Functors – Custom functions
STL Containers
1) vector
Dynamic array that grows automatically.
#include <vector>
vector<int> v = {1, 2, 3};
v.push_back(4);
- Fast access using
[]
. - Supports
push_back
,pop_back
,insert
, etc.
2) list
Doubly linked list
#include <list>
list<int> l = {10, 20, 30};
l.push_front(5); l.push_back(40);
- Allows fast insert/delete at both ends.
- No random access.
3) deque
Double-ended queue, faster than vector for front insertions.
#include <deque>
deque<int> d = {1, 2, 3};
d.push_front(0); d.push_back(4);
4) array
Fixed Size array.
#include <array>
array<int, 5> arr = {1, 2, 3, 4, 5};
- Use
at()
,front()
,back()
5) forward_list
Singly linked list, optimized for low memory.
#include <forward_list>
forward_list<int> fl = {1, 2, 3};
- Only
push_front
supported.
6) set
Sorted Unique Elements.
#include <set>
set<int> s = {3, 1, 4};
s.insert(2);
O(log n)
operations.
7) multiset
Allows duplicate elements.
#include <set>
multiset<int> ms = {1, 2, 2, 3};
8) map
Stores key-value pairs, unique keys, sorted by keys.
#include <map>
map<string, int> age;
age["Alice"] = 25;
age["Bob"] = 30;
9) multimap
Same as map, but allows duplicate keys.
#include <map>
multimap<string, int> mm;
mm.insert({"apple", 3});
mm.insert({"apple", 5});
10) unordered_set, unordered_map
Use hashing instead of sorting. Average O(1) operations.
#include <unordered_set>
#include <unordered_map>
unordered_set<int> us = {5, 10, 15};
unordered_map<string, int> um = {{"a", 1}, {"b", 2}};
11) stack, queue, priority_queue
#include <stack>
#include <queue>
// Stack (LIFO - Last In First Out)
stack<int> st;
st.push(10); st.pop();
// Queue (FIFO - First In First Out)
queue<int> q;
q.push(10); q.pop();
// Priority Queue (Max Heap by default)
priority_queue<int> pq;
pq.push(10); pq.push(5);
Quick Summary:
- Sequence Containers
Container | Header | Description | Access | Time Complexity |
---|---|---|---|---|
vector |
<vector> |
Dynamic array | Random | O(1) access, O(n) insert at front |
list |
<list> |
Doubly linked list | Sequential | O(1) insert/delete at ends |
deque |
<deque> |
Double-ended queue | Random | O(1) at both ends |
array |
<array> |
Fixed-size array (C++11) | Random | O(1) |
forward_list |
<forward_list> |
Singly linked list (C++11) | Forward only | O(1) front ops |
- Associative Containers
Container | Header | Key | Ordered | Time Complexity |
---|---|---|---|---|
set |
<set> |
Unique | Yes | O(log n) |
multiset |
<set> |
Duplicates | Yes | O(log n) |
map |
<map> |
Unique keys | Yes | O(log n) |
multimap |
<map> |
Duplicate keys | Yes | O(log n) |
- Unordered Associative Containers
Container | Header | Ordered? | Time Complexity |
---|---|---|---|
unordered_set |
<unordered_set> |
No | O(1) average |
unordered_map |
<unordered_map> |
No | O(1) average |
- Container Adapters
Container | Header | Description | Time Complexity |
---|---|---|---|
stack |
<stack> |
LIFO | O(1) for push/pop |
queue |
<queue> |
FIFO | O(1) for push/pop |
priority_queue |
<queue> |
Max-Heap (default) | O(log n) |
STL Algorithms
#include <algorithm>
Use with containers via iterators.
Common STL Algorithms
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
count(v.begin(), v.end(), 3);
find(v.begin(), v.end(), 4);
Example
vector<int> v = {5, 2, 8, 1};
sort(v.begin(), v.end()); // Sorted: 1 2 5 8
Quick Summary
Algorithm | Function | Time Complexity |
---|---|---|
Sorting | sort(v.begin(), v.end()) |
O(n log n) |
Searching | find() / binary_search() |
O(n) / O(log n) |
Reversal | reverse() |
O(n) |
Counting | count() |
O(n) |
Max/Min | max_element() / min_element() |
O(n) |
Iterators - Navigating Containers
Iterators act like pointers.
Example
vector<int> v = {10, 20, 30};
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
You can also use auto
to simplify:
for (auto it = v.begin(); it != v.end(); ++it)
Functors - Function Objects
Functors are objects that behave like functions. They are used in custom sorting and algorithms.
Example
struct Square {
int operator()(int x) {
return x * x;
}
};
Square sq;
cout << sq(4); // Output: 16
Advanced STL Features
-
lower_bound() and upper_bound(): Binary search helpers (on sorted containers).
-
emplace() and emplace_back(): Faster than insertions, constructs in-place.
-
Lambda Functions with STL algorithms.
sort(v.begin(), v.end(), [](int a, int b){
return a > b;
});
Quick Tips
-
Use
#include <bits/stdc++.h>
in contests only. -
Use
auto
for iterators in modern C++. -
Always
sort()
beforelower_bound()
orbinary_search()
.
STL Flashcards
Q1. What is the default underlying container of a stack
in STL?
A: deque
Q2. How do you implement an LRU cache using STL?
A: Combine unordered_map
with list
Q3. Difference between map
and unordered_map
?
A: map
uses balanced trees (O(log n))
, unordered_map
uses hash table (O(1)
avg)
Q4. Which container is best for range queries and ordering?
A: set
or map
Q5. Can we use set
to remove duplicates from a vector?
A: Yes
Q6. Is vector
thread-safe?
A: No
Q7. What is the difference between emplace()
and insert()
?
A: emplace()
constructs in-place (faster, avoids copy)
Q8. Which data structure is used in priority_queue
by default?
A: Max-heap (binary heap)
Q9. Which STL containers are not sorted by default?
A: unordered_set
, unordered_map
, vector
, list
Q10. How to create a min heap with STL?
A: Use priority_queue<int, vector<int>, greater<int>>
Conclusion
Mastering STL is like unlocking a cheat code in C++. It's fast, expressive, and powerful. Practice each container to understand internal behavior and performance trade-offs.
That`s all for this blog.....
For more amazing content..Click here
Until next time..
Happy Coding!!!
7 Reactions
1 Bookmarks