C++ algorithm library
The C++ Standard Library’s <algorithm>
header provides a comprehensive suite of functions designed to operate on sequences of elements, commonly referred to as ranges. These functions facilitate a wide array of operations, including searching, sorting, counting, and manipulating data.
Non-Modifying Sequence Operations
Function | Description |
---|---|
std::all_of | Returns true if all elements in the range satisfy the given predicate. |
std::any_of | Returns true if any element in the range satisfies the given predicate. |
std::none_of | Returns true if no elements in the range satisfy the given predicate. |
std::for_each | Applies a function to each element in the range. |
std::find | Searches for the first occurrence of a specified value in the range. |
std::find_if | Searches for the first element in the range that satisfies the given predicate. |
std::find_if_not | Searches for the first element in the range that does not satisfy the given predicate. |
std::find_end | Searches for the last occurrence of a subsequence in the range. |
std::find_first_of | Searches for the first element in the range that matches any element from another range. |
std::adjacent_find | Searches for the first occurrence of consecutive identical elements in the range. |
std::count | Returns the number of elements in the range that are equal to a specified value. |
std::count_if | Returns the number of elements in the range that satisfy the given predicate. |
std::mismatch | Finds the first position where two ranges differ. |
std::equal | Checks if two ranges are equal. |
std::is_permutation | Checks if one range is a permutation of another. |
std::search | Searches for the first occurrence of a subsequence within the range. |
std::search_n | Searches for the first occurrence of n consecutive elements that are equal to a specified value. |
Modifying Sequence Operations
Function | Description |
---|---|
std::copy | Copies elements from one range to another. |
std::copy_n | Copies a specified number of elements from one range to another. |
std::copy_if | Copies elements from a range that satisfy a given predicate. |
std::copy_backward | Copies elements from one range to another, starting from the end. |
std::move | Moves elements from one range to another. |
std::move_backward | Moves elements from one range to another, starting from the end. |
std::swap | Exchanges the values of two objects. |
std::swap_ranges | Exchanges the values of two ranges. |
std::iter_swap | Exchanges the values of the elements pointed to by two iterators. |
std::transform | Applies a function to each element in a range and writes the result to another range. |
std::replace | Replaces all occurrences of a value in a range with another value. |
std::replace_if | Replaces elements in a range that satisfy a given predicate with another value. |
std::replace_copy | Copies a range, replacing all occurrences of a value in the new range. |
std::replace_copy_if | Copies a range, replacing elements that satisfy a given predicate in the new range. |
std::fill | Fills a range with a specified value. |
std::fill_n | Fills a specified number of elements in a range with a value. |
std::generate | Generates values for a range using a function. |
std::generate_n | Generates values for a specified number of elements in a range using a function. |
std::remove | Removes elements with a specific value from a range (logically, without changing the container size). |
std::remove_if | Removes elements from a range that satisfy a given predicate. |
std::remove_copy | Copies a range, excluding elements with a specific value. |
std::remove_copy_if | Copies a range, excluding elements that satisfy a given predicate. |
std::unique | Removes consecutive duplicate elements in a range. |
std::unique_copy | Copies a range, removing consecutive duplicates in the new range. |
std::reverse | Reverses the elements in a range. |
std::reverse_copy | Copies a range, reversing the elements in the new range. |
std::rotate | Rotates the elements in a range to the left. |
std::rotate_copy | Copies a range, rotating the elements in the new range to the left. |
std::random_shuffle | Randomly rearranges elements in a range. |
std::shuffle | Randomly rearranges elements in a range using a specified random generator. |
Partitions
Function | Description |
---|---|
std::is_partitioned | Tests whether the range is partitioned according to a specified predicate. |
std::partition | Rearranges the elements in the range so that elements satisfying a predicate appear before those that do not. |
std::stable_partition | Partitions the range into two groups while preserving the relative order of elements in each group. |
std::partition_copy | Copies elements from the input range into two separate ranges based on a predicate. |
std::partition_point | Returns an iterator to the partition point of a range that has already been partitioned. |
Sorting
Function | Description |
---|---|
std::sort | Sorts the elements in the range in ascending order. |
std::stable_sort | Sorts the elements in the range while preserving the relative order of equivalent elements. |
std::partial_sort | Sorts the first n elements in the range, leaving the rest unsorted. |
std::partial_sort_copy | Copies and partially sorts the elements from the input range to another range. |
std::is_sorted | Checks if the elements in the range are sorted. |
std::is_sorted_until | Finds the first unsorted element in the range. |
std::nth_element | Rearranges the elements in the range so that the nth element is in its sorted position. |
Binary Search (Operating on Partitioned/Sorted Ranges)
Function | Description |
---|---|
std::lower_bound | Returns an iterator to the first element that is not less than the specified value in a sorted range. |
std::upper_bound | Returns an iterator to the first element that is greater than the specified value in a sorted range. |
std::equal_range | Returns a pair of iterators defining the subrange of elements that are equal to a specified value. |
std::binary_search | Tests if a specified value exists in a sorted sequence. |
Merge (Operating on Sorted Ranges)
Function | Description |
---|---|
std::merge | Merges two sorted ranges into a single sorted range. |
std::inplace_merge | Merges two consecutive sorted ranges within the same container. |
std::includes | Checks if one sorted range includes all elements of another sorted range. |
std::set_union | Computes the union of two sorted ranges. |
std::set_intersection | Computes the intersection of two sorted ranges. |
std::set_difference | Computes the difference of two sorted ranges. |
std::set_symmetric_difference | Computes the symmetric difference of two sorted ranges. |
Heap Operations
Function | Description |
---|---|
std::push_heap | Inserts an element into a heap range and maintains the heap property. |
std::pop_heap | Removes the largest element from a heap range. |
std::make_heap | Transforms a range into a valid heap. |
std::sort_heap | Sorts the elements of a heap in ascending order. |
std::is_heap | Checks if a range satisfies the heap property. |
std::is_heap_until | Finds the first element in a range that violates the heap property. |
Min/Max Operations
Function | Description |
---|---|
std::min | Returns the smallest of two or more values. |
std::max | Returns the largest of two or more values. |
std::minmax | Returns the smallest and largest of two or more values as a pair. |
std::min_element | Returns an iterator to the smallest element in a range. |
std::max_element | Returns an iterator to the largest element in a range. |
std::minmax_element | Returns iterators to the smallest and largest elements in a range as a pair. |
Other Operations
Function | Description |
---|---|
std::lexicographical_compare | Compares two ranges lexicographically. |
std::next_permutation | Transforms the range into the next permutation in lexicographical order. |
std::prev_permutation | Transforms the range into the previous permutation in lexicographical order. |
Key Features of the algorithm Library
- Non-Modifying Sequence Operations: Functions that analyze or search ranges without altering their elements.
- Modifying Sequence Operations: Functions that modify the elements within a range.
- Sorting and Related Operations: Functions that order elements within a range or perform operations related to sorting.
- Set Operations: Functions that perform standard set operations on sorted ranges.
- Heap Operations: Functions that manage heap structures within ranges.
- Minimum and Maximum Operations: Functions that determine the smallest or largest elements in a range.