The Standard Template Library (STL) is a powerful library in C++ that provides ready-to-use classes and functions for common data structures and algorithms.
It saves time by providing generic containers, iterators, and algorithms which can be reused for different data types.
- Containers — Data structures to store objects
- Algorithms — Functions that operate on containers
- Iterators — Objects that point to container elements and allow traversal
- Function Objects (Functors) — Objects acting like functions
- Adapters — Containers with restricted interfaces (stack, queue, etc.)
Containers store collections of objects. They can be:
| Container | Description | Header | Example Declaration |
|---|---|---|---|
vector |
Dynamic array (resizable) | <vector> |
vector<int> v; |
list |
Doubly linked list | <list> |
list<int> l; |
deque |
Double-ended queue (fast front/back) | <deque> |
deque<int> d; |
set |
Sorted unique elements | <set> |
set<int> s; |
map |
Key-value pairs (sorted by key) | <map> |
map<string, int> m; |
unordered_map |
Key-value pairs, no order | <unordered_map> |
unordered_map<int, string> um; |
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
v.push_back(4); // Add element at end
cout << v[0] << endl; // Access element by index
cout << v.size() << endl; // Get size of vector
}Algorithms are predefined functions to perform operations on containers:
| Algorithm | Description | Header |
|---|---|---|
sort() |
Sort elements | <algorithm> |
reverse() |
Reverse elements | <algorithm> |
find() |
Find element | <algorithm> |
count() |
Count occurrences | <algorithm> |
max_element() |
Find max element | <algorithm> |
min_element() |
Find min element | <algorithm> |
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {4, 2, 5, 1, 3};
sort(v.begin(), v.end()); // Sort the vector
for (int x : v) cout << x << " "; // Print sorted vector
cout << endl;
// Find element 3
auto it = find(v.begin(), v.end(), 3);
if (it != v.end()) cout << "Found 3 at index " << (it - v.begin()) << endl;
}Iterators are pointers or objects that traverse containers. They allow algorithms to work on different containers uniformly.
| Iterator | Description |
|---|---|
begin() |
Points to first element |
end() |
Points just past the last element |
rbegin() |
Reverse iterator at last element |
rend() |
Reverse iterator before first element |
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> v = {10, 20, 30};
for (auto it = v.begin(); it != v.end(); ++it) {
cout << *it << " "; // Dereference iterator to get element
}
cout << endl;
}Function objects are objects that behave like functions by overloading the operator(). Used for custom behavior in algorithms.
Adapters provide restricted interfaces on top of other containers:
| Adapter | Description | Header |
|---|---|---|
stack |
Last-in-first-out (LIFO) | <stack> |
queue |
First-in-first-out (FIFO) | <queue> |
priority_queue |
Elements with priority | <queue> |
#include <stack>
#include <iostream>
using namespace std;
int main() {
stack<int> s;
s.push(1);
s.push(2);
cout << s.top() << endl; // Output: 2
s.pop();
cout << s.top() << endl; // Output: 1
}| Component | Purpose | Common Headers |
|---|---|---|
| Containers | Store data | <vector>, <list>, <map>, <set> |
| Algorithms | Perform operations | <algorithm> |
| Iterators | Traverse containers | Provided by container headers |
| Function Objects | Custom callable objects | <functional> |
| Adapters | Restricted interface containers | <stack>, <queue> |
- STL makes code cleaner, faster, and easier to maintain.
- Use
autokeyword with iterators for simplicity. - Algorithms use iterators, so they work with any container supporting those iterators.
- Learn containers and algorithms gradually with practice.
Happy coding with STL! 🚀
Created by ChatGPT
Ask me anytime for more examples or explanations!