STL - Algorithms
Tags:
cpp
stl
algorithms
References
I prefer C++17
Sorting operations
Function | Use |
---|---|
sort | Sorts a range (ascending order by default) |
stable_sort | Sorts a range of elements while preserving order between equal elements |
partial_sort | Sorts the first N elements of a range |
is_sorted | Check whether range is sorted |
is_sorted_until | Finds the largest sorted subrange |
nth_element | Partially sorts the range making sure that it’s partitioned by the given element |
Comparison functions
Function | Use |
---|---|
greater<T> | Checks whether the first argument is greater than the second |
less<T> | Checks whether the first argument is lesser than the second |
lexicographical_compare | Check whether the first range is lexicographically lesser than the second |
Binary search (on partitioned/sorted ranges)
Function | Use |
---|---|
lower_bound | Returns iterator pointing to the first element not less than the given value |
upper_bound | Returns iterator pointing to the first element greater than a certain value |
equal_range | Returns range of elements matching a specific key |
binary_search | Determines if an element exists in a partially-ordered range |
Minimum/Maximum operations
Function | Use |
---|---|
max | Returns larger of given values |
min | Return smaller of given values |
min_element | Returns smallest element in range |
max_element | Returns largest element in range |
minmax | Returns lowest and the greatest of given values |
minmax_element | Returns smallest and largest elements in range |
Heap operations
By default, the heap behavior is for max-heap
Function | Use |
---|---|
push_heap | Insert element into heap |
pop_heap | Pop topmost element from heap |
make_heap | Make heap from range |
sort_heap | Turns the heap into a range of sorted elements (default ascending order) |
is_heap | Checks if given range is a max-heap |
is_heap_until | Finds the largest subrange that is a max-heap |
Permutation operations
Function | Use |
---|---|
next_permutation | Generates the next greater lexicographic permutation of a range of elements |
prev_permutation | Generates the next smaller lexicographic permutation of a range of elements |
is_permutation | Checks if a sequence is a permutation of another sequence |
Partitioning Operations
Function | Use |
---|---|
is_partitioned | Test whether range is partitioned by the given predicate |
partition | Divides a range of elements into two groups |
partition_copy | Copies a range dividing the elements into two groups |
stable_partition | Divides elements into two groups while preserving their relative order |
partition_point | Locates the partition point of a partitioned range |
Set, Merge operations (on sorted ranges)
Function | Use |
---|---|
includes | Checks if one sequence is a subsequence of another |
set_union | Computes the union of two sets |
set_intersection | Computes the intersection of two sets |
set_difference | Computes the difference between two sets |
set_symmetric_difference | Computes the symmetric difference between two sets |
merge | Merges two sorted ranges |
inplace_merge | Merges two ordered ranges in-place |
Non-modifying sequence operations
Function | Use |
---|---|
for_each | Apply function to a range of elements |
for_each_n | Apply function to first N elements of a sequence |
all_of , any_of , none_of | Check if condition is true for all/any/none of the elements in a range |
find , find_if , find_if_not | Find the first element satisfying specific criteria |
find_end | Find last sequence of elements in a certain range |
find_first_of | Searches for any one of a set of elements |
adjacent_find | Finds the first two adjacent items that are equal (or satisfy given condition) |
count , count_if | Returns number of elements satisfying specific criteria |
mismatch | Finds the first position where two ranges differ |
equal | Checks if two sets of elements are the same |
search | Searches for first occurrence of a range of elements |
search_n | Searches for first occurrence of a number consecutive copies of an element in a range |
Modifying sequence operations
Function | Use |
---|---|
copy , copy_if | Copies a range of elements to a new location |
copy_n | Copies a number of elements to a new location |
copy_backward | Copy range of elements backward |
move | Moves a range of elements to a new location |
move_backward | Moves a range of elements to a new location in backwards order |
swap | Swaps the values of two objects |
swap_ranges | Swaps two ranges of elements |
iter_swap | Swaps the elements pointed to by two iterators |
transform | Applies a function to a range, storing results in a destination range |
replace , replace_if | Replaces all values satisfying specific criteria with another value |
replace_copy , replace_copy_if | Copies a range, replacing elements satisfying specific criteria with another value |
fill | Copy-assigns the given value to every element in a range |
fill_n | Copy-assigns the given value to N elements in a range |
generate | Assigns the results of successive function calls to every element in range |
generate_n | Assigns the results of successive function calls to N elements in range |
remove , remove_if | Removes elements satisfying specific criteria |
remove_copy , remove_copy_if | Copies a range of elements omitting those that satisfy specific criteria |
unique | Removes consecutive duplicate elements in range |
unique_copy | Creates a copy of some range of elements that contains no consecutive duplicates |
reverse | Reverses the order of elements in a range |
reverse_copy | Creates a copy of a range that is reversed |
rotate | Rotates the order of elements in a range |
rotate_copy | Copies and rotate a range of elements |
shuffle | Randomly re-orders elements in a range |
sample | Selects N random elements from a sequence |