What is STL in C++?
The Standard Template Library (STL) is one of the most powerful features of C++, providing a collection of ready-made, efficient, and versatile classes and functions. It offers a wide range of data structures and algorithms that significantly enhance the development process.
- STL is a library of generic classes and functions.
- It provides a set of common data structures and algorithms.
- It is part of the C++ Standard Library.
- STL is designed using templates, making it highly flexible.
Key Components of STL:
- Containers: Store data.
- Algorithms: Operate on data.
- Iterators: Provide access to data.
Why Use STL?
- Saves development time with pre-built data structures.
- Highly optimized for performance.
- Supports generic programming (works with any data type).
- Provides consistent and tested implementations.
Core Components of STL
1. Containers
Containers are data structures that store collections of objects.
Types of Containers:
- Sequence Containers: Store elements in a linear sequence.
- Associative Containers: Store elements in a sorted order with keys.
- Unordered Containers: Store elements in an unordered manner.
- Container Adapters: Provide restricted interfaces over other containers.
2. Algorithms
- Pre-built functions that perform operations on data (searching, sorting, modifying).
- Work seamlessly with STL containers.
3. Iterators
- Provide a way to access container elements.
- Act like pointers for container elements.
Categories of Containers
1. Sequence Containers
- Store data in a linear manner.
- Allow element insertion and access in a specific order.
| Container | Description | Header |
|---|
| vector | Dynamic array | <vector> |
| deque | Double-ended queue | <deque> |
| list | Doubly linked list | <list> |
| forward_list | Singly linked list | <forward_list> |
| array | Static array (fixed size) | <array> |
| string | Sequence of characters (text) | <string> |
➡️ Example: Vector
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3, 4, 5};
v.push_back(6); // Add element at the end
cout << "Vector elements: ";
for (int x : v) {
cout << x << " ";
}
return 0;
}
Output:
Vector elements: 1 2 3 4 5 6
2. Associative Containers
- Store elements in a sorted order (based on keys).
- Provide fast search, insertion, and deletion.
| Container | Description | Header |
|---|
| set | Stores unique sorted elements | <set> |
| multiset | Allows duplicate sorted elements | <set> |
| map | Key-value pairs (unique keys) | <map> |
| multimap | Key-value pairs (duplicate keys) | <map> |
➡️ Example: Map
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> marks;
marks["Alice"] = 90;
marks["Bob"] = 85;
marks["Charlie"] = 88;
cout << "Marks of students:\n";
for (auto &pair : marks) {
cout << pair.first << ": " << pair.second << endl;
}
return 0;
}
Output:
Marks of students:
Alice: 90
Bob: 85
Charlie: 88
3. Unordered Containers
- Store elements without any specific order.
- Faster than associative containers for insertions and lookups.
| Container | Description | Header |
|---|
| unordered_set | Stores unique unordered elements | <unordered_set> |
| unordered_multiset | Allows duplicate unordered elements | <unordered_set> |
| unordered_map | Unordered key-value pairs | <unordered_map> |
| unordered_multimap | Unordered key-value pairs with duplicate keys | <unordered_map> |
➡️ Example: Unordered Set
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_set<int> s = {1, 2, 3, 3, 4, 5};
s.insert(6);
cout << "Unordered Set: ";
for (int x : s) {
cout << x << " ";
}
return 0;
}
Output (Order may vary):
Unordered Set: 1 2 3 4 5 6
4. Container Adapters
- Provide restricted interfaces over other containers.
- Do not directly store data.
| Adapter | Description | Underlying Container | Header |
|---|
| stack | LIFO (Last-In, First-Out) | deque or vector | <stack> |
| queue | FIFO (First-In, First-Out) | deque or list | <queue> |
| priority_queue | Max-heap (highest value first) | vector | <queue> |
➡️ Example: Stack
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
cout << "Stack elements (LIFO): ";
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
return 0;
}
Output:
Stack elements (LIFO): 30 20 10
STL Iterators
- Iterators are objects that point to elements in a container.
- They provide a way to traverse containers (like pointers).
Types of Iterators:
- Input Iterator: Reads data (one direction).
- Output Iterator: Writes data (one direction).
- Forward Iterator: Reads/writes (one direction).
- Bidirectional Iterator: Reads/writes (two directions).
- Random Access Iterator: Reads/writes (direct access, like array).
Syntax:
vector<int>::iterator it;
➡️ Example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40, 50};
vector<int>::iterator it;
cout << "Using iterator: ";
for (it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
return 0;
}
Output:
Using iterator: 10 20 30 40 50
STL Algorithms
- Algorithms are functions that perform operations on data (sorting, searching, etc.).
- Available in <algorithm> and <numeric> headers.
Common Algorithms:
- sort(): Sorts elements in a range.
- reverse(): Reverses elements in a range.
- find(): Searches for an element.
- accumulate(): Computes sum of a range.
➡️ Example:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {5, 1, 4, 2, 3};
sort(v.begin(), v.end());
cout << "Sorted vector: ";
for (int x : v) {
cout << x << " ";
}
return 0;
}
Output:
Sorted vector: 1 2 3 4 5