Constrained algorithms (since C++20)

From cppreference.com
< cpp‎ | algorithm
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
(C++11)(C++11)(C++11)
(C++17)
Modifying sequence operations
Partitioning operations
Sorting operations
(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
(C++11)
Minimum/maximum operations
(C++11)
(C++17)

Permutations
Numeric operations
Operations on uninitialized storage
(C++17)
(C++17)
(C++17)
C library
 
Constrained algorithms
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutations
Numeric operations
Fold operations
Operations on uninitialized storage
Return types
 

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

Constrained algorithms

Defined in header <algorithm>
Defined in namespace std::ranges
Non-modifying sequence operations
checks if a predicate is true for all, any or none of the elements in a range
(niebloid)
applies a function to a range of elements
(niebloid)
applies a function object to the first n elements of a sequence
(niebloid)
returns the number of elements satisfying specific criteria
(niebloid)
finds the first position where two ranges differ
(niebloid)
determines if two sets of elements are the same
(niebloid)
returns true if one range is lexicographically less than another
(niebloid)
finds the first element satisfying specific criteria
(niebloid)
finds the last element satisfying specific criteria
(niebloid)
finds the last sequence of elements in a certain range
(niebloid)
searches for any one of a set of elements
(niebloid)
finds the first two adjacent items that are equal (or satisfy a given predicate)
(niebloid)
searches for a range of elements
(niebloid)
searches for a number consecutive copies of an element in a range
(niebloid)
checks if the range contains the given element or subrange
(niebloid)
checks whether a range starts with another range
(niebloid)
checks whether a range ends with another range
(niebloid)
Modifying sequence operations
copies a range of elements to a new location
(niebloid)
copies a number of elements to a new location
(niebloid)
copies a range of elements in backwards order
(niebloid)
moves a range of elements to a new location
(niebloid)
moves a range of elements to a new location in backwards order
(niebloid)
assigns a range of elements a certain value
(niebloid)
assigns a value to a number of elements
(niebloid)
applies a function to a range of elements
(niebloid)
saves the result of a function in a range
(niebloid)
saves the result of N applications of a function
(niebloid)
removes elements satisfying specific criteria
(niebloid)
copies a range of elements omitting those that satisfy specific criteria
(niebloid)
replaces all values satisfying specific criteria with another value
(niebloid)
copies a range, replacing elements satisfying specific criteria with another value
(niebloid)
swaps two ranges of elements
(niebloid)
reverses the order of elements in a range
(niebloid)
creates a copy of a range that is reversed
(niebloid)
rotates the order of elements in a range
(niebloid)
copies and rotate a range of elements
(niebloid)
randomly re-orders elements in a range
(niebloid)
shifts elements in a range
(niebloid)
selects n random elements from a sequence
(niebloid)
removes consecutive duplicate elements in a range
(niebloid)
creates a copy of some range of elements that contains no consecutive duplicates
(niebloid)
Partitioning operations
determines if the range is partitioned by the given predicate
(niebloid)
divides a range of elements into two groups
(niebloid)
copies a range dividing the elements into two groups
(niebloid)
divides elements into two groups while preserving their relative order
(niebloid)
locates the partition point of a partitioned range
(niebloid)
Sorting operations
checks whether a range is sorted into ascending order
(niebloid)
finds the largest sorted subrange
(niebloid)
sorts a range into ascending order
(niebloid)
sorts the first N elements of a range
(niebloid)
copies and partially sorts a range of elements
(niebloid)
sorts a range of elements while preserving order between equal elements
(niebloid)
partially sorts the given range making sure that it is partitioned by the given element
(niebloid)
Binary search operations (on sorted ranges)
returns an iterator to the first element not less than the given value
(niebloid)
returns an iterator to the first element greater than a certain value
(niebloid)
determines if an element exists in a partially-ordered range
(niebloid)
returns range of elements matching a specific key
(niebloid)
Set operations (on sorted ranges)
merges two sorted ranges
(niebloid)
merges two ordered ranges in-place
(niebloid)
returns true if one sequence is a subsequence of another
(niebloid)
computes the difference between two sets
(niebloid)
computes the intersection of two sets
(niebloid)
computes the symmetric difference between two sets
(niebloid)
computes the union of two sets
(niebloid)
Heap operations
checks if the given range is a max heap
(niebloid)
finds the largest subrange that is a max heap
(niebloid)
creates a max heap out of a range of elements
(niebloid)
adds an element to a max heap
(niebloid)
removes the largest element from a max heap
(niebloid)
turns a max heap into a range of elements sorted in ascending order
(niebloid)
Minimum/maximum operations
returns the greater of the given values
(niebloid)
returns the largest element in a range
(niebloid)
returns the smaller of the given values
(niebloid)
returns the smallest element in a range
(niebloid)
returns the smaller and larger of two elements
(niebloid)
returns the smallest and the largest elements in a range
(niebloid)
clamps a value between a pair of boundary values
(niebloid)
Permutation operations
determines if a sequence is a permutation of another sequence
(niebloid)
generates the next greater lexicographic permutation of a range of elements
(niebloid)
generates the next smaller lexicographic permutation of a range of elements
(niebloid)

Constrained numeric operations

Defined in header <numeric>
Defined in namespace std::ranges
fills a range with successive increments of the starting value
(niebloid)

Constrained fold operations

Defined in header <algorithm>
Defined in namespace std::ranges
left-folds a range of elements
(niebloid)
left-folds a range of elements using the first element as an initial value
(niebloid)
right-folds a range of elements
(niebloid)
right-folds a range of elements using the last element as an initial value
(niebloid)
left-folds a range of elements, and returns a pair (iterator, value)
(niebloid)
left-folds a range of elements using the first element as an initial value, and returns a pair (iterator, optional)
(niebloid)

Constrained uninitialized memory algorithms

Defined in header <memory>
Defined in namespace std::ranges
copies a range of objects to an uninitialized area of memory
(niebloid)
copies a number of objects to an uninitialized area of memory
(niebloid)
copies an object to an uninitialized area of memory, defined by a range
(niebloid)
copies an object to an uninitialized area of memory, defined by a start and a count
(niebloid)
moves a range of objects to an uninitialized area of memory
(niebloid)
moves a number of objects to an uninitialized area of memory
(niebloid)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range
(niebloid)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and count
(niebloid)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range
(niebloid)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
(niebloid)
destroys a range of objects
(niebloid)
destroys a number of objects in a range
(niebloid)
destroys an object at a given address
(niebloid)
creates an object at a given address
(niebloid)

Return types

Defined in header <algorithm>
Defined in namespace std::ranges
provides a way to store an iterator and a function object as a single unit
(class template)
provides a way to store two iterators as a single unit
(class template)
provides a way to store two iterators as a single unit
(class template)
provides a way to store three iterators as a single unit
(class template)
provides a way to store three iterators as a single unit
(class template)
provides a way to store two objects or references of the same type as a single unit
(class template)
provides a way to store an iterator and a boolean flag as a single unit
(class template)
provides a way to store an iterator and a value as a single unit
(class template)
provides a way to store an iterator and a value as a single unit
(class template)

Notes

Feature-test macro Value Std Comment
__cpp_lib_ranges 201911L (C++20) Ranges library and constrained algorithms
__cpp_lib_ranges_contains 202207L (C++23) std::ranges::contains
__cpp_lib_ranges_find_last 202207L (C++23) std::ranges::find_last
__cpp_lib_ranges_fold 202207L (C++23) std::ranges fold algorithms
__cpp_lib_ranges_iota 202202L (C++23) std::ranges::iota
__cpp_lib_ranges_starts_ends_with 202106L (C++23) std::ranges::starts_with, std::ranges::ends_with
__cpp_lib_shift 201806L (C++20) std::shift_left and std::shift_right
202202L (C++23) std::ranges::shift_left and std::ranges::shift_right