The Standard Template Library (STL) provides reusable data structures and algorithms for storing and processing data. It uses templates to support different data types.
- Provides containers such as vectors, lists, stacks, and maps.
- Supports generic algorithms that work with compatible iterator ranges..
Components of STL
The components of STL are the features provided by STL in C++ that can be classified into 3 types:

Containers
Containers are template-based data structures that store and organize elements. They provide member functions for operations such as insertion, deletion and accessing elements.
Containers can be further classified into 4 types:
- Sequence Containers: Vector, Deque, List, Forward List, Array
- Container Adaptors: Stack, Queue, Priority Queue
- Associative Containers: Set, Multiset, Map, Multimap
- Unordered Associative Containers: Unordered Set, Unordered Multiset, Unordered Map, Unordered Multimap
Algorithms
STL Algorithms provide generic functions for operations such as sorting, searching, counting, and modifying elements.
Most common algorithms are defined in <algorithm> and <numeric>. They have specified complexity guarantees, but their suitability depends on the data structure and use case.
- Sort: Arranges elements in ascending order (default).
- Binary Search: Checks whether a value exists in a sorted range.
- Find: Searches for the first occurrence of a given value.
- Count: Counts how many times a value appears in the given range.
- Reverse: Reverses the order of elements in the given range.
- Accumulate: Computes the sum of all elements in the range.
- Unique: Removes consecutive duplicate elements.
- Lower bound: Returns iterator to the first element âĨ value in a sorted range.
- Upper bound: Returns iterator to the first element > value in a sorted range.
- Replace: Replaces all occurrences of old value with new value in the given range.
Iterators
Iterators provide a common way to access and traverse elements stored in STL containers.
- Support operations such as accessing and moving between elements.
- Connect containers with generic algorithms.
- Have different capabilities depending on their iterator category
Example: Program to demonstrates how containers, algorithms and iterators work together to sort elements and remove duplicates.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// Container to store integers
vector<int> numbers = {4, 2, 2, 3, 1, 3, 3};
// Sort elements
sort(numbers.begin(), numbers.end());
// Move duplicates outside the logical range
auto newEnd = unique(numbers.begin(), numbers.end());
// Remove remaining elements
numbers.erase(newEnd, numbers.end());
cout << "Unique sorted elements: ";
// Traverse using an iterator
for (auto it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << " ";
}
return 0;
}
Output
Unique sorted elements: 1 2 3 4
Explanation:
- vector stores the integer elements.
- sort() arranges the elements in ascending order.
- unique() moves consecutive duplicates outside the logical range, and erase() removes them from the vector.
- The iterator traverses and prints the remaining elements.
Benefits of STL
- Reduces development time through reusable components.
- Provides specified time complexity guarantees for operations.
- Enables compile-time type checking through templates.
- Reduces the need to implement common data structures and algorithms manually.