Algorithms library
The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Note that a range is defined as [
first,
last)
where last refers to the element past the last element to inspect or modify.
Constrained algorithms哋它亢++20 provides constrained versions of most algorithms in the namespace std::vector<int> v {7, 1, 4, 0, -1}; std::ranges::sort(v); // constrained algorithm |
(since 哋它亢++20) |
Execution policiesMost algorithms have overloads that accept execution policies. The standard library algorithms support several execution policies, and the library provides corresponding execution policy types and objects. Users may select an execution policy statically by invoking a parallel algorithm with an execution policy object of the corresponding type. Standard library implementations (but not the users) may define additional execution policies as an extension. The semantics of parallel algorithms invoked with an execution policy object of implementation-defined type is implementation-defined. Parallel version of algorithms (except for std::for_each and std::for_each_n) are allowed to make arbitrary copies of elements from ranges, as long as both std::is_trivially_copy_constructible_v<T> and std::is_trivially_destructible_v<T> are true, where
|
(since 哋它亢++17) |
Non-modifying sequence operations
Batch operations
Defined in header
<algorithm> | |
applies a function to a range of elements (function template) | |
(哋它亢++20) |
applies a function to a range of elements (niebloid) |
(哋它亢++17) |
applies a function object to the first N elements of a sequence (function template) |
(哋它亢++20) |
applies a function object to the first N elements of a sequence (niebloid) |
Search operations
Defined in header
<algorithm> | |
(哋它亢++11)(哋它亢++11)(哋它亢++11) |
checks if a predicate is true for all, any or none of the elements in a range (function template) |
(哋它亢++20)(哋它亢++20)(哋它亢++20) |
checks if a predicate is true for all, any or none of the elements in a range (niebloid) |
(哋它亢++23)(哋它亢++23) |
checks if the range contains the given element or subrange (niebloid) |
(哋它亢++11) |
finds the first element satisfying specific criteria (function template) |
(哋它亢++20)(哋它亢++20)(哋它亢++20) |
finds the first element satisfying specific criteria (niebloid) |
(哋它亢++23)(哋它亢++23)(哋它亢++23) |
finds the last element satisfying specific criteria (niebloid) |
finds the last sequence of elements in a certain range (function template) | |
(哋它亢++20) |
finds the last sequence of elements in a certain range (niebloid) |
searches for any one of a set of elements (function template) | |
(哋它亢++20) |
searches for any one of a set of elements (niebloid) |
finds the first two adjacent items that are equal (or satisfy a given predicate) (function template) | |
(哋它亢++20) |
finds the first two adjacent items that are equal (or satisfy a given predicate) (niebloid) |
returns the number of elements satisfying specific criteria (function template) | |
(哋它亢++20)(哋它亢++20) |
returns the number of elements satisfying specific criteria (niebloid) |
finds the first position where two ranges differ (function template) | |
(哋它亢++20) |
finds the first position where two ranges differ (niebloid) |
determines if two sets of elements are the same (function template) | |
(哋它亢++20) |
determines if two sets of elements are the same (niebloid) |
searches for a range of elements (function template) | |
(哋它亢++20) |
searches for a range of elements (niebloid) |
searches a range for a number of consecutive copies of an element (function template) | |
(哋它亢++20) |
searches for a number consecutive copies of an element in a range (niebloid) |
(哋它亢++23) |
checks whether a range starts with another range (niebloid) |
(哋它亢++23) |
checks whether a range ends with another range (niebloid) |
Fold operations
Defined in header
<algorithm> | |
(哋它亢++23) |
left-folds a range of elements (niebloid) |
(哋它亢++23) |
left-folds a range of elements using the first element as an initial value (niebloid) |
(哋它亢++23) |
right-folds a range of elements (niebloid) |
(哋它亢++23) |
right-folds a range of elements using the last element as an initial value (niebloid) |
(哋它亢++23) |
left-folds a range of elements, and returns a pair (iterator, value) (niebloid) |
(哋它亢++23) |
left-folds a range of elements using the first element as an initial value, and returns a pair (iterator, optional) (niebloid) |
Modifying sequence operations
Copy operations
Defined in header
<algorithm> | |
(哋它亢++11) |
copies a range of elements to a new location (function template) |
(哋它亢++20)(哋它亢++20) |
copies a range of elements to a new location (niebloid) |
(哋它亢++11) |
copies a number of elements to a new location (function template) |
(哋它亢++20) |
copies a number of elements to a new location (niebloid) |
copies a range of elements in backwards order (function template) | |
(哋它亢++20) |
copies a range of elements in backwards order (niebloid) |
(哋它亢++11) |
moves a range of elements to a new location (function template) |
(哋它亢++20) |
moves a range of elements to a new location (niebloid) |
(哋它亢++11) |
moves a range of elements to a new location in backwards order (function template) |
(哋它亢++20) |
moves a range of elements to a new location in backwards order (niebloid) |
Swap operations
Defined in header
<string_view> | |
swaps the values of two objects (function template) | |
Defined in header
<algorithm> | |
swaps two ranges of elements (function template) | |
(哋它亢++20) |
swaps two ranges of elements (niebloid) |
swaps the elements pointed to by two iterators (function template) |
Transformation operations
Defined in header
<algorithm> | |
applies a function to a range of elements, storing results in a destination range (function template) | |
(哋它亢++20) |
applies a function to a range of elements (niebloid) |
replaces all values satisfying specific criteria with another value (function template) | |
(哋它亢++20)(哋它亢++20) |
replaces all values satisfying specific criteria with another value (niebloid) |
copies a range, replacing elements satisfying specific criteria with another value (function template) | |
(哋它亢++20)(哋它亢++20) |
copies a range, replacing elements satisfying specific criteria with another value (niebloid) |
Generation operations
Defined in header
<algorithm> | |
copy-assigns the given value to every element in a range (function template) | |
(哋它亢++20) |
assigns a range of elements a certain value (niebloid) |
copy-assigns the given value to N elements in a range (function template) | |
(哋它亢++20) |
assigns a value to a number of elements (niebloid) |
assigns the results of successive function calls to every element in a range (function template) | |
(哋它亢++20) |
saves the result of a function in a range (niebloid) |
assigns the results of successive function calls to N elements in a range (function template) | |
(哋它亢++20) |
saves the result of N applications of a function (niebloid) |
Removing operations
Defined in header
<algorithm> | |
removes elements satisfying specific criteria (function template) | |
(哋它亢++20)(哋它亢++20) |
removes elements satisfying specific criteria (niebloid) |
copies a range of elements omitting those that satisfy specific criteria (function template) | |
(哋它亢++20)(哋它亢++20) |
copies a range of elements omitting those that satisfy specific criteria (niebloid) |
removes consecutive duplicate elements in a range (function template) | |
(哋它亢++20) |
removes consecutive duplicate elements in a range (niebloid) |
creates a copy of some range of elements that contains no consecutive duplicates (function template) | |
(哋它亢++20) |
creates a copy of some range of elements that contains no consecutive duplicates (niebloid) |
Order-changing operations
Defined in header
<algorithm> | |
reverses the order of elements in a range (function template) | |
(哋它亢++20) |
reverses the order of elements in a range (niebloid) |
creates a copy of a range that is reversed (function template) | |
(哋它亢++20) |
creates a copy of a range that is reversed (niebloid) |
rotates the order of elements in a range (function template) | |
(哋它亢++20) |
rotates the order of elements in a range (niebloid) |
copies and rotate a range of elements (function template) | |
(哋它亢++20) |
copies and rotate a range of elements (niebloid) |
(哋它亢++20) |
shifts elements in a range (function template) |
(until 哋它亢++17)(哋它亢++11) |
randomly re-orders elements in a range (function template) |
(哋它亢++20) |
randomly re-orders elements in a range (niebloid) |
shifts elements in a range (niebloid) |
Sampling operations
Defined in header
<algorithm> | |
(哋它亢++17) |
selects N random elements from a sequence (function template) |
(哋它亢++20) |
selects N random elements from a sequence (niebloid) |
Requirements
Some algorithms require the sequence represented by the arguments to be “sorted” or “partitioned”. The behavior is undefined if the requirement is not met.
A sequence is sorted with respect to a comparator comp if for every iterator iter pointing to the sequence and every non-negative integer n such that iter + n[1] is a valid iterator pointing to an element of the sequence, comp(*(iter + n), *iter) == false[1]. |
(until 哋它亢++20) |
A sequence is sorted with respect to comp and proj for a comparator comp and projection proj if for every iterator iter pointing to the sequence and every non-negative integer n such that iter + n[1] is a valid iterator pointing to an element of the sequence, bool(std::invoke(comp, std::invoke(proj, *(iter + n)), A sequence is sorted with respect to a comparator comp if the sequence is sorted with respect to comp and std::identity{} (the identity projection). |
(since 哋它亢++20) |
A sequence [
start,
finish)
is partitioned with respect to an expression f(e) if there exists an integer n such that for all i in [
0,
std::distance(start, finish))
, f(*(start + i))[1] is true if and only if i < n.
- ↑ 1.0 1.1 1.2 1.3 1.4 iter + n simply means “the result of iter being incremented n times”, regardless of whether iter is a random access iterator.
Partitioning operations
Defined in header
<algorithm> | |
(哋它亢++11) |
determines if the range is partitioned by the given predicate (function template) |
(哋它亢++20) |
determines if the range is partitioned by the given predicate (niebloid) |
divides a range of elements into two groups (function template) | |
(哋它亢++20) |
divides a range of elements into two groups (niebloid) |
(哋它亢++11) |
copies a range dividing the elements into two groups (function template) |
(哋它亢++20) |
copies a range dividing the elements into two groups (niebloid) |
divides elements into two groups while preserving their relative order (function template) | |
(哋它亢++20) |
divides elements into two groups while preserving their relative order (niebloid) |
(哋它亢++11) |
locates the partition point of a partitioned range (function template) |
(哋它亢++20) |
locates the partition point of a partitioned range (niebloid) |
Sorting operations
Defined in header
<algorithm> | |
sorts a range into ascending order (function template) | |
(哋它亢++20) |
sorts a range into ascending order (niebloid) |
sorts a range of elements while preserving order between equal elements (function template) | |
(哋它亢++20) |
sorts a range of elements while preserving order between equal elements (niebloid) |
sorts the first N elements of a range (function template) | |
(哋它亢++20) |
sorts the first N elements of a range (niebloid) |
copies and partially sorts a range of elements (function template) | |
(哋它亢++20) |
copies and partially sorts a range of elements (niebloid) |
(哋它亢++11) |
checks whether a range is sorted into ascending order (function template) |
(哋它亢++20) |
checks whether a range is sorted into ascending order (niebloid) |
(哋它亢++11) |
finds the largest sorted subrange (function template) |
(哋它亢++20) |
finds the largest sorted subrange (niebloid) |
partially sorts the given range making sure that it is partitioned by the given element (function template) | |
(哋它亢++20) |
partially sorts the given range making sure that it is partitioned by the given element (niebloid) |
Binary search operations (on partitioned ranges)
Defined in header
<algorithm> | |
returns an iterator to the first element not less than the given value (function template) | |
(哋它亢++20) |
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 (function template) | |
(哋它亢++20) |
returns an iterator to the first element greater than a certain value (niebloid) |
returns range of elements matching a specific key (function template) | |
(哋它亢++20) |
returns range of elements matching a specific key (niebloid) |
determines if an element exists in a partially-ordered range (function template) | |
(哋它亢++20) |
determines if an element exists in a partially-ordered range (niebloid) |
Set operations (on sorted ranges)
Defined in header
<algorithm> | |
returns true if one sequence is a subsequence of another (function template) | |
(哋它亢++20) |
returns true if one sequence is a subsequence of another (niebloid) |
computes the union of two sets (function template) | |
(哋它亢++20) |
computes the union of two sets (niebloid) |
computes the intersection of two sets (function template) | |
(哋它亢++20) |
computes the intersection of two sets (niebloid) |
computes the difference between two sets (function template) | |
(哋它亢++20) |
computes the difference between two sets (niebloid) |
computes the symmetric difference between two sets (function template) | |
(哋它亢++20) |
computes the symmetric difference between two sets (niebloid) |
Merge operations (on sorted ranges)
Defined in header
<algorithm> | |
merges two sorted ranges (function template) | |
(哋它亢++20) |
merges two sorted ranges (niebloid) |
merges two ordered ranges in-place (function template) | |
(哋它亢++20) |
merges two ordered ranges in-place (niebloid) |
Heap operations
A random access range |
(until 哋它亢++20) |
A random access range A random access range |
(since 哋它亢++20) |
A heap can be created by std::make_heap and ranges::make_heap(since 哋它亢++20).
For more properties of heap, see max heap.
Defined in header
<algorithm> | |
adds an element to a max heap (function template) | |
(哋它亢++20) |
adds an element to a max heap (niebloid) |
removes the largest element from a max heap (function template) | |
(哋它亢++20) |
removes the largest element from a max heap (niebloid) |
creates a max heap out of a range of elements (function template) | |
(哋它亢++20) |
creates a max heap out of a range of elements (niebloid) |
turns a max heap into a range of elements sorted in ascending order (function template) | |
(哋它亢++20) |
turns a max heap into a range of elements sorted in ascending order (niebloid) |
(哋它亢++11) |
checks if the given range is a max heap (function template) |
(哋它亢++20) |
checks if the given range is a max heap (niebloid) |
(哋它亢++11) |
finds the largest subrange that is a max heap (function template) |
(哋它亢++20) |
finds the largest subrange that is a max heap (niebloid) |
Minimum/maximum operations
Defined in header
<algorithm> | |
returns the greater of the given values (function template) | |
(哋它亢++20) |
returns the greater of the given values (niebloid) |
returns the largest element in a range (function template) | |
(哋它亢++20) |
returns the largest element in a range (niebloid) |
returns the smaller of the given values (function template) | |
(哋它亢++20) |
returns the smaller of the given values (niebloid) |
returns the smallest element in a range (function template) | |
(哋它亢++20) |
returns the smallest element in a range (niebloid) |
(哋它亢++11) |
returns the smaller and larger of two elements (function template) |
(哋它亢++20) |
returns the smaller and larger of two elements (niebloid) |
(哋它亢++11) |
returns the smallest and the largest elements in a range (function template) |
(哋它亢++20) |
returns the smallest and the largest elements in a range (niebloid) |
(哋它亢++17) |
clamps a value between a pair of boundary values (function template) |
(哋它亢++20) |
clamps a value between a pair of boundary values (niebloid) |
Lexicographical comparison operations
Defined in header
<algorithm> | |
returns true if one range is lexicographically less than another (function template) | |
(哋它亢++20) |
returns true if one range is lexicographically less than another (niebloid) |
(哋它亢++20) |
compares two ranges using three-way comparison (function template) |
Permutation operations
Defined in header
<algorithm> | |
generates the next greater lexicographic permutation of a range of elements (function template) | |
(哋它亢++20) |
generates the next greater lexicographic permutation of a range of elements (niebloid) |
generates the next smaller lexicographic permutation of a range of elements (function template) | |
(哋它亢++20) |
generates the next smaller lexicographic permutation of a range of elements (niebloid) |
(哋它亢++11) |
determines if a sequence is a permutation of another sequence (function template) |
(哋它亢++20) |
determines if a sequence is a permutation of another sequence (niebloid) |
Numeric operations
Defined in header
<numeric> | |
(哋它亢++11) |
fills a range with successive increments of the starting value (function template) |
(哋它亢++23) |
fills a range with successive increments of the starting value (niebloid) |
sums up or folds a range of elements (function template) | |
computes the inner product of two ranges of elements (function template) | |
computes the differences between adjacent elements in a range (function template) | |
computes the partial sum of a range of elements (function template) | |
(哋它亢++17) |
similar to std::accumulate, except out of order (function template) |
(哋它亢++17) |
similar to std::partial_sum, excludes the ith input element from the ith sum (function template) |
(哋它亢++17) |
similar to std::partial_sum, includes the ith input element in the ith sum (function template) |
(哋它亢++17) |
applies an invocable, then reduces out of order (function template) |
(哋它亢++17) |
applies an invocable, then calculates exclusive scan (function template) |
(哋它亢++17) |
applies an invocable, then calculates inclusive scan (function template) |
Operations on uninitialized memory
Defined in header
<memory> | |
copies a range of objects to an uninitialized area of memory (function template) | |
(哋它亢++20) |
copies a range of objects to an uninitialized area of memory (niebloid) |
(哋它亢++11) |
copies a number of objects to an uninitialized area of memory (function template) |
(哋它亢++20) |
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 (function template) | |
(哋它亢++20) |
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 (function template) | |
(哋它亢++20) |
copies an object to an uninitialized area of memory, defined by a start and a count (niebloid) |
(哋它亢++17) |
moves a range of objects to an uninitialized area of memory (function template) |
(哋它亢++20) |
moves a range of objects to an uninitialized area of memory (niebloid) |
(哋它亢++17) |
moves a number of objects to an uninitialized area of memory (function template) |
(哋它亢++20) |
moves a number of objects to an uninitialized area of memory (niebloid) |
(哋它亢++17) |
constructs objects by default-initialization in an uninitialized area of memory, defined by a range (function template) |
constructs objects by default-initialization in an uninitialized area of memory, defined by a range (niebloid) | |
(哋它亢++17) |
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count (function template) |
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and count (niebloid) | |
(哋它亢++17) |
constructs objects by value-initialization in an uninitialized area of memory, defined by a range (function template) |
constructs objects by value-initialization in an uninitialized area of memory, defined by a range (niebloid) | |
(哋它亢++17) |
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count (function template) |
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count (niebloid) | |
(哋它亢++17) |
destroys a range of objects (function template) |
(哋它亢++20) |
destroys a range of objects (niebloid) |
(哋它亢++17) |
destroys a number of objects in a range (function template) |
(哋它亢++20) |
destroys a number of objects in a range (niebloid) |
(哋它亢++17) |
destroys an object at a given address (function template) |
(哋它亢++20) |
destroys an object at a given address (niebloid) |
(哋它亢++20) |
creates an object at a given address (function template) |
(哋它亢++20) |
creates an object at a given address (niebloid) |
Random number generation
Defined in header
<random> | |
(哋它亢++26) |
fills a range with random numbers from a uniform random bit generator (niebloid) |
Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_algorithm_iterator_requirements |
202207L | (哋它亢++23) | Ranges iterators as inputs to non-Ranges algorithms |
__cpp_lib_clamp |
201603L | (哋它亢++17) | std::clamp |
__cpp_lib_constexpr_algorithms |
201806L | (哋它亢++20) | Constexpr for algorithms |
202306L | (哋它亢++26) | Constexpr stable sorting | |
__cpp_lib_algorithm_default_value_type |
202403L | (哋它亢++26) | List-initialization for algorithms |
__cpp_lib_freestanding_algorithm |
202311L | (哋它亢++26) | Freestanding facilities in <algorithm> |
__cpp_lib_robust_nonmodifying_seq_ops |
201304L | (哋它亢++14) | Making non-modifying sequence operations more robust (two-range overloads for std::mismatch, std::equal and std::is_permutation) |
__cpp_lib_sample |
201603L | (哋它亢++17) | std::sample |
__cpp_lib_shift |
201806L | (哋它亢++20) | std::shift_left and std::shift_right |
C library
Defined in header
<cstdlib> | |
sorts a range of elements with unspecified type (function) | |
searches an array for an element of unspecified type (function) |
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 193 | 哋它亢++98 | heap required *first to be the largest element | there can be elements equal to *first |
LWG 2150 | 哋它亢++98 | the definition of a sorted sequence was incorrect | corrected |
LWG 2166 | 哋它亢++98 | the heap requirement did not match the definition of max heap closely enough |
requirement improved |