# Constrained algorithms (since C++20)

From cppreference.com
< cpp‎ | algorithm

C++20 provides constrained versions of most algorithms in the namespace `std::ranges`. In these algorithms, a range can be specified as either a `Iterator`-`Sentinel` pair or as a single `Range` argument, and projections and pointer-to-member callables are supported. Additionally, the return type of most algorithms have been changed to return all potentially useful information computed during the execution of the algorithm.

### Algorithm concepts and utilities

The header `<iterator>` provides a set of concepts and related utility templates designed to ease constraining common algorithm operations.

Defined in header `<iterator>`
Defined in namespace `std`
##### Indirect callable concepts
IndirectUnaryInvocable IndirectRegularUnaryInvocable | specifies that a callable type can be invoked with the result of dereferencing a `Readable` type (concept)
IndirectUnaryPredicate | specifies that a callable type, when invoked with the result of dereferencing a `Readable` type, satisfies `Predicate` (concept)
IndirectRelation | specifies that a callable type, when invoked with the result of dereferencing two `Readable` types, satisfies `Predicate` (concept)
IndirectStrictWeakOrder | specifies that a callable type, when invoked with the result of dereferencing two `Readable` types, satisfies `StrictWeakOrder` (concept)
##### Common algorithm requirements
IndirectlyMovable | specifies that values may be moved from a `Readable` type to a `Writable` type (concept)
IndirectlyMovableStorable | specifies that values may be moved from a `Readable` type to a `Writable` type and that the move may be performed via an intermediate object (concept)
IndirectlyCopyable | specifies that values may be copied from a `Readable` type to a `Writable` type (concept)
IndirectlyCopyableStorable | specifies that values may be copied from a `Readable` type to a `Writable` type and that the copy may be performed via an intermediate object (concept)
IndirectlySwappable | specifies that the values referenced by two `Readable` types can be swapped (concept)
IndirectlyComparable | specifies that the values referenced by two `Readable` types can be compared (concept)
Permutable | specifies the common requirements of algorithms that reorder elements in place (concept)
Mergeable | specifies the requirements of algorithms that merge sorted sequences into an output sequence by copying elements (concept)
Sortable | specifies the common requirements of algorithms that permute sequences into ordered sequences (concept)
##### Utilities
indirect_result_t | computes the result of invoking a callable object on the result of dereferencing some set of `Readable` types (alias template)
projected | helper template for specifying the constraints on algorithms that accept projections (class template)

### Constrained algorithms

Defined in header `<algorithm>`
Defined in namespace `std::ranges`
##### Non-modifying sequence operations
all_of any_of none_of | checks if a predicate is true for all, any or none of the elements in a range (niebloid)
for_each | applies a function to a range of elements (niebloid)
count count_if | returns the number of elements satisfying specific criteria (niebloid)
mismatch | finds the first position where two ranges differ (niebloid)
equal | determines if two sets of elements are the same (niebloid)
lexicographical_compare | returns true if one range is lexicographically less than another (niebloid)
find find_if find_if_not | finds the first element satisfying specific criteria (niebloid)
find_end | finds the last sequence of elements in a certain range (niebloid)
find_first_of | searches for any one of a set of elements (niebloid)
adjacent_find | finds the first two adjacent items that are equal (or satisfy a given predicate) (niebloid)
search | searches for a range of elements (niebloid)
search_n | searches for a number consecutive copies of an element in a range (niebloid)
##### Modifying sequence operations
copy copy_if | copies a range of elements to a new location (niebloid)
copy_n | copies a number of elements to a new location (niebloid)
copy_backward | copies a range of elements in backwards order (niebloid)
move | moves a range of elements to a new location (niebloid)
move_backward | moves a range of elements to a new location in backwards order (niebloid)
fill | assigns a range of elements a certain value (niebloid)
fill_n | assigns a value to a number of elements (niebloid)
transform | applies a function to a range of elements (niebloid)
generate | saves the result of a function in a range (niebloid)
generate_n | saves the result of N applications of a function (niebloid)
remove remove_if | removes elements satisfying specific criteria (niebloid)
remove_copy remove_copy_if | copies a range of elements omitting those that satisfy specific criteria (niebloid)
replace replace_if | replaces all values satisfying specific criteria with another value (niebloid)
replace_copy replace_copy_if | copies a range, replacing elements satisfying specific criteria with another value (niebloid)
swap_ranges | swaps two ranges of elements (niebloid)
reverse | reverses the order of elements in a range (niebloid)
reverse_copy | creates a copy of a range that is reversed (niebloid)
rotate | rotates the order of elements in a range (niebloid)
rotate_copy | copies and rotate a range of elements (niebloid)
shuffle | randomly re-orders elements in a range (niebloid)
unique | removes consecutive duplicate elements in a range (niebloid)
unique_copy | creates a copy of some range of elements that contains no consecutive duplicates (niebloid)
##### Partitioning operations
is_partitioned | determines if the range is partitioned by the given predicate (niebloid)
partition | divides a range of elements into two groups (niebloid)
partition_copy | copies a range dividing the elements into two groups (niebloid)
stable_partition | divides elements into two groups while preserving their relative order (niebloid)
partition_point | locates the partition point of a partitioned range (niebloid)
##### Sorting operations
is_sorted | checks whether a range is sorted into ascending order (niebloid)
is_sorted_until | finds the largest sorted subrange (niebloid)
sort | sorts a range into ascending order (niebloid)
partial_sort | sorts the first N elements of a range (niebloid)
partial_sort_copy | copies and partially sorts a range of elements (niebloid)
stable_sort | sorts a range of elements while preserving order between equal elements (niebloid)
nth_element | partially sorts the given range making sure that it is partitioned by the given element (niebloid)
##### Binary search operations (on sorted ranges)
lower_bound | returns an iterator to the first element not less than the given value (niebloid)
upper_bound | returns an iterator to the first element greater than a certain value (niebloid)
binary_search | determines if an element exists in a certain range (niebloid)
equal_range | returns range of elements matching a specific key (niebloid)
##### Set operations (on sorted ranges)
merge | merges two sorted ranges (niebloid)
inplace_merge | merges two ordered ranges in-place (niebloid)
includes | returns true if one set is a subset of another (niebloid)
set_difference | computes the difference between two sets (niebloid)
set_intersection | computes the intersection of two sets (niebloid)
set_symmetric_difference | computes the symmetric difference between two sets (niebloid)
set_union | computes the union of two sets (niebloid)
##### Heap operations
is_heap | checks if the given range is a max heap (niebloid)
is_heap_until | finds the largest subrange that is a max heap (niebloid)
make_heap | creates a max heap out of a range of elements (niebloid)
push_heap | adds an element to a max heap (niebloid)
pop_heap | removes the largest element from a max heap (niebloid)
sort_heap | turns a max heap into a range of elements sorted in ascending order (niebloid)
##### Minimum/maximum operations
max | returns the greater of the given values (niebloid)
max_element | returns the largest element in a range (niebloid)
min | returns the smaller of the given values (niebloid)
min_element | returns the smallest element in a range (niebloid)
minmax | returns the smaller and larger of two elements (niebloid)
minmax_element | returns the smallest and the largest elements in a range (niebloid)
##### Permutation operations
is_permutation | determines if a sequence is a permutation of another sequence (niebloid)
next_permutation | generates the next greater lexicographic permutation of a range of elements (niebloid)
prev_permutation | generates the next smaller lexicographic permutation of a range of elements (niebloid)

### Constrained uninitialized memory algorithms

Defined in header `<memory>`
Defined in namespace `std::ranges`
uninitialized_copy (C++20) | copies a range of objects to an uninitialized area of memory (niebloid)
uninitialized_copy_n (C++20) | copies a number of objects to an uninitialized area of memory (niebloid)
uninitialized_fill (C++20) | copies an object to an uninitialized area of memory, defined by a range (niebloid)
uninitialized_fill_n (C++20) | copies an object to an uninitialized area of memory, defined by a start and a count (niebloid)
uninitialized_move (C++20) | moves a range of objects to an uninitialized area of memory (niebloid)
uninitialized_move_n (C++20) | moves a number of objects to an uninitialized area of memory (niebloid)
uninitialized_default_construct (C++20) | constructs objects by default-initialization in an uninitialized area of memory, defined by a range (niebloid)
uninitialized_default_construct_n (C++20) | constructs objects by default-initialization in an uninitialized area of memory, defined by a start and count (niebloid)
uninitialized_value_construct (C++20) | constructs objects by value-initialization in an uninitialized area of memory, defined by a range (niebloid)
uninitialized_value_construct_n (C++20) | constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count (niebloid)
destroy_at (C++20) | destroys an object at a given address (niebloid)
destroy (C++20) | destroys a range of objects (niebloid)
destroy_n (C++20) | destroys a number of objects in a range (niebloid)

Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges&oldid=108056"