STL - Algorithms


Tags:  cppstlalgorithms

References

I prefer C++17

Sorting operations

FunctionUse
sortSorts a range (ascending order by default)
stable_sortSorts a range of elements while preserving order between equal elements
partial_sortSorts the first N elements of a range
is_sortedCheck whether range is sorted
is_sorted_untilFinds the largest sorted subrange
nth_elementPartially sorts the range making sure that it’s partitioned by the given element

Comparison functions

FunctionUse
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_compareCheck whether the first range is lexicographically lesser than the second

Binary search (on partitioned/sorted ranges)

FunctionUse
lower_boundReturns iterator pointing to the first element not less than the given value
upper_boundReturns iterator pointing to the first element greater than a certain value
equal_rangeReturns range of elements matching a specific key
binary_searchDetermines if an element exists in a partially-ordered range

Minimum/Maximum operations

FunctionUse
maxReturns larger of given values
minReturn smaller of given values
min_elementReturns smallest element in range
max_elementReturns largest element in range
minmaxReturns lowest and the greatest of given values
minmax_elementReturns smallest and largest elements in range

Heap operations

By default, the heap behavior is for max-heap

FunctionUse
push_heapInsert element into heap
pop_heapPop topmost element from heap
make_heapMake heap from range
sort_heapTurns the heap into a range of sorted elements (default ascending order)
is_heapChecks if given range is a max-heap
is_heap_untilFinds the largest subrange that is a max-heap

Permutation operations

FunctionUse
next_permutationGenerates the next greater lexicographic permutation of a range of elements
prev_permutationGenerates the next smaller lexicographic permutation of a range of elements
is_permutationChecks if a sequence is a permutation of another sequence

Partitioning Operations

FunctionUse
is_partitionedTest whether range is partitioned by the given predicate
partitionDivides a range of elements into two groups
partition_copyCopies a range dividing the elements into two groups
stable_partitionDivides elements into two groups while preserving their relative order
partition_pointLocates the partition point of a partitioned range

Set, Merge operations (on sorted ranges)

FunctionUse
includesChecks if one sequence is a subsequence of another
set_unionComputes the union of two sets
set_intersectionComputes the intersection of two sets
set_differenceComputes the difference between two sets
set_symmetric_differenceComputes the symmetric difference between two sets
mergeMerges two sorted ranges
inplace_mergeMerges two ordered ranges in-place

Non-modifying sequence operations

FunctionUse
for_eachApply function to a range of elements
for_each_nApply function to first N elements of a sequence
all_of, any_of, none_ofCheck if condition is true for all/any/none of the elements in a range
find, find_if, find_if_notFind the first element satisfying specific criteria
find_endFind last sequence of elements in a certain range
find_first_ofSearches for any one of a set of elements
adjacent_findFinds the first two adjacent items that are equal (or satisfy given condition)
count, count_ifReturns number of elements satisfying specific criteria
mismatchFinds the first position where two ranges differ
equalChecks if two sets of elements are the same
searchSearches for first occurrence of a range of elements
search_nSearches for first occurrence of a number consecutive copies of an element in a range

Modifying sequence operations

FunctionUse
copy, copy_ifCopies a range of elements to a new location
copy_nCopies a number of elements to a new location
copy_backwardCopy range of elements backward
moveMoves a range of elements to a new location
move_backwardMoves a range of elements to a new location in backwards order
swapSwaps the values of two objects
swap_rangesSwaps two ranges of elements
iter_swapSwaps the elements pointed to by two iterators
transformApplies a function to a range, storing results in a destination range
replace, replace_ifReplaces all values satisfying specific criteria with another value
replace_copy, replace_copy_ifCopies a range, replacing elements satisfying specific criteria with another value
fillCopy-assigns the given value to every element in a range
fill_nCopy-assigns the given value to N elements in a range
generateAssigns the results of successive function calls to every element in range
generate_nAssigns the results of successive function calls to N elements in range
remove, remove_ifRemoves elements satisfying specific criteria
remove_copy, remove_copy_ifCopies a range of elements omitting those that satisfy specific criteria
uniqueRemoves consecutive duplicate elements in range
unique_copyCreates a copy of some range of elements that contains no consecutive duplicates
reverseReverses the order of elements in a range
reverse_copyCreates a copy of a range that is reversed
rotateRotates the order of elements in a range
rotate_copyCopies and rotate a range of elements
shuffleRandomly re-orders elements in a range
sampleSelects N random elements from a sequence