Algorithms (STL)
In C++, the Standard Template Library (STL) provides a collection of algorithms that operate on various containers and sequences.
These algorithms are part of the C++ Standard Library and offer a wide range of functionalities for data manipulation, searching, sorting, and more.
STL algorithms are generic and can be used with different data structures thanks to the use of iterators.
Table of Contents
STL Algorithms:
Non-modifying Sequence Operations:
std::all_of
,std::any_of
,std::none_of
: Check if all, any, or none of the elements satisfy a specific condition.std::count
,std::count_if
: Count the occurrences of a value or elements that satisfy a condition.std::find
,std::find_if
,std::find_if_not
: Find the first occurrence of a value or the first element that satisfies a condition.std::equal
,std::mismatch
: Check if two sequences are equal or find the first mismatched element.std::search
,std::find_end
: Search for the first occurrence of a sequence or the last occurrence of a subsequence.std::adjacent_find
: Find the first occurrence of two adjacent elements that are equal.
Modifying Sequence Operations:
std::copy
,std::copy_if
,std::copy_n
,std::copy_backward
: Copy elements from one range to another.std::move
,std::move_backward
: Move elements from one range to another.std::fill
,std::fill_n
: Assign values to elements in a range or a specific number of elements.std::generate
,std::generate_n
: Generate values for elements in a range or a specific number of elements.std::transform
: Apply a function to each element in a range and store the result in another range.
Partitioning Operations:
std::partition
,std::partition_copy
,std::stable_partition
: Rearrange elements based on a condition.std::is_partitioned
: Check if a range is partitioned based on a condition.std::partition_point
: Find the partition point in a partitioned range.
Sorting Operations:
std::sort
,std::partial_sort
,std::stable_sort
,std::nth_element
: Sort elements in a range.std::is_sorted
,std::is_sorted_until
: Check if a range is sorted or find the first unsorted element.
Binary Search Operations:
std::binary_search
: Check if a value exists in a sorted range.std::lower_bound
,std::upper_bound
: Find the lower/upper bound of a value in a sorted range.std::equal_range
: Find the range of elements matching a specific value in a sorted range.
Merge Operations:
std::merge
,std::inplace_merge
: Merge two sorted ranges into one.std::includes
: Check if one sorted range includes another.
Set Operations:
std::set_union
,std::set_intersection
,std::set_difference
,std::set_symmetric_difference
: Perform set operations on sorted ranges.
Heap Operations:
std::make_heap
,std::push_heap
,std::pop_heap
,std::sort_heap
: Create and manipulate heaps (max heap by default).
Min and Max Operations:
std::min
,std::max
,std::min_element
,std::max_element
: Get the minimum or maximum value or element in a range.
Numeric Operations:
- Search Operations:
std::find_first_of
: Find the first occurrence of any element from a set of values.std::search_n
: Find the first occurrence of a subsequence of a certain size.
- Query Operations:
std::equal_to
,std::not_equal_to
,std::greater
,std::greater_equal
,std::less
,std::less_equal
: Function objects for comparison operations.std::identity
,std::logical_not
,std::logical_and
,std::logical_or
,std::multiplies
,std::plus
,std::minus
,std::divides
,std::modulus
,std::negate
: Function objects for arithmetic and logical operations.
These are some of the commonly used STL algorithms. They allow developers to perform various data manipulations and transformations without the need to write custom loops for each operation, making the code more concise, efficient, and readable. STL algorithms operate on containers using iterators, providing a uniform interface that works with different data structures.