STL Algorithms
Algorithms in C++ STL
The C++ Standard Template Library (STL) provides a rich set of algorithms that work on containers. These algorithms are implemented as template functions and can perform a variety of tasks such as searching, sorting, manipulating, and more. The key advantage is that the same algorithm can work with different types of containers.
Video Explanation

Categories of Algorithms
Algorithms in STL can be broadly divided into the following categories:
- Sorting Algorithms
- Searching Algorithms
- Modifying Algorithms
- Non-Modifying Algorithms
- Partitioning Algorithms
- Set Operations
- Min/Max Operations
- Heap Operations
1. Sorting Algorithms
Sorting algorithms rearrange elements in a container in a specific order. The most commonly used sorting algorithm in STL is sort().
Common Functions:
sort(): Sorts a range of elements in ascending order by default.partial_sort(): Sorts the first N elements.stable_sort(): Sorts while preserving the relative order of equivalent elements.nth_element(): Reorders the elements so that the element at the Nth position is in the sorted order.
Example:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {5, 2, 9, 1, 5, 6};
std::sort(v.begin(), v.end()); // Sort in ascending order
for (int i : v)
std::cout << i << " ";
return 0;
}
2. Searching Algorithms
Searching algorithms help you find elements in a container.