The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on
of elements.
(since C++20)
C++20 provides
versions of most algorithms in the namespace std::ranges. In these algorithms, a range can be specified as either an
-
pair or as a single
argument, and projections and pointer-to-member callables are supported. Additionally, the
of most algorithms have been changed to return all potentially useful information computed during the execution of the algorithm.
std::vector<int>v{7,1,4,0,-1};std::ranges::sort(v);// constrained algorithmParallel algorithms (since C++17)
A parallel algorithm is a function template in the algorithms library with a template parameter named ExecutionPolicy or constrained by
(since C++26). Such a template parameter is termed an execution policy template parameter , it describes the manner in which the execution of a parallel algorithm may be parallelized.
Unless otherwise stated, parallel algorithms 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 T is the type of elements.
Execution policies
The standard library algorithms support several
, and the library provides corresponding execution policy types and objects. Users may select an execution policy statically by invoking a parallel algorithm with an
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.
Defined in header
Defined in namespace std::execution
sequenced_policyparallel_policyparallel_unsequenced_policyunsequenced_policy
(C++17)(C++17)(C++17)(C++20)
execution policy types
(class)
(C++17)(C++17)(C++17)(C++20)
global execution policy objects
(constant)
Defined in namespace std
(C++17)
test whether a class represents an execution policy
(class template)
(C++26)
specifies that a type represents an execution policy
(exposition-only concept*)
Non-modifying sequence operations
Batch operations
Search operations
Defined in header
(C++11)(C++11)(C++11)
checks if a predicate is true for all, any or none of the elements in a range
(function template & algorithm function object)
ranges::all_ofranges::any_ofranges::none_of
(C++20)(C++20)(C++20)
ranges::containsranges::contains_subrange
(C++23)(C++23)
checks if the range contains the given element or subrange
(algorithm function object)
(C++11)
finds the first element satisfying specific criteria
(function template & algorithm function object)
ranges::findranges::find_ifranges::find_if_not
(C++20)(C++20)(C++20)
ranges::find_lastranges::find_last_ifranges::find_last_if_not
(C++23)(C++23)(C++23)
finds the last element satisfying specific criteria
(algorithm function object)
finds the last sequence of elements in a certain range
(function template & algorithm function object)
(C++20)
searches for any one of a set of elements
(function template & algorithm function object)
(C++20)
finds the first two adjacent items that are equal (or satisfy a given predicate)
(function template & algorithm function object)
(C++20)
returns the number of elements satisfying specific criteria
(function template & algorithm function object)
(C++20)(C++20)
finds the first position where two ranges differ
(function template & algorithm function object)
(C++20)
determines if two sets of elements are the same
(function template & algorithm function object)
(C++20)
searches for the first occurrence of a range of elements
(function template & algorithm function object)
(C++20)
searches for the first occurrence of a number consecutive copies of an element in a range
(function template & algorithm function object)
(C++20)
(C++23)
checks whether a range starts with another range
(algorithm function object)
(C++23)
checks whether a range ends with another range
(algorithm function object)
Fold operations (since C++23)
Defined in header
indirectly-binary-left-foldableindirectly-binary-right-foldable
(C++23)(C++23)
specifies that a callable type can be used in fold algorithms
(exposition-only concept*)
(C++23)
left-folds a range of elements
(algorithm function object)
(C++23)
left-folds a range of elements using the first element as an initial value
(algorithm function object)
(C++23)
right-folds a range of elements
(algorithm function object)
(C++23)
right-folds a range of elements using the last element as an initial value
(algorithm function object)
(C++23)
left-folds a range of elements, and returns a
(iterator, value)
(algorithm function object)
ranges::fold_left_first_with_iter
(C++23)
left-folds a range of elements using the first element as an initial value, and returns a
(iterator,
)
(algorithm function object)
Modifying sequence operations
Copy operations
Defined in header
(C++11)
copies a range of elements to a new location
(function template & algorithm function object)
(C++20)(C++20)
(C++11)
copies a number of elements to a new location
(function template & algorithm function object)
(C++20)
copies a range of elements in backwards order
(function template & algorithm function object)
(C++20)
(C++11)
moves a range of elements to a new location
(function template & algorithm function object)
(C++20)
(C++11)
moves a range of elements to a new location in backwards order
(function template & algorithm function object)
(C++20)
Swap operations
Defined in header
(until C++11)
Defined in header
(since C++11)
Defined in header
swaps the values of two objects
(function template)
Defined in header
swaps two ranges of elements
(function template & algorithm function object)
(C++20)
swaps the elements pointed to by two iterators
(function template)
Transformation operations
Defined in header
applies a function to a range of elements, storing results in a destination range
(function template & algorithm function object)
(C++20)
replaces all values satisfying specific criteria with another value
(function template & algorithm function object)
ranges::replaceranges::replace_if
(C++20)(C++20)
copies a range, replacing elements satisfying specific criteria with another value
(function template & algorithm function object)
ranges::replace_copyranges::replace_copy_if
(C++20)(C++20)
Generation operations
Defined in header
copy-assigns the given value to every element in a range
(function template & algorithm function object)
(C++20)
copy-assigns the given value to N elements in a range
(function template & algorithm function object)
(C++20)
assigns the results of successive function calls to every element in a range
(function template & algorithm function object)
(C++20)
assigns the results of successive function calls to N elements in a range
(function template & algorithm function object)
(C++20)
Removing operations
Defined in header
removes elements satisfying specific criteria
(function template & algorithm function object)
ranges::removeranges::remove_if
(C++20)(C++20)
copies a range of elements omitting those that satisfy specific criteria
(function template & algorithm function object)
ranges::remove_copyranges::remove_copy_if
(C++20)(C++20)
removes consecutive duplicate elements in a range
(function template & algorithm function object)
(C++20)
creates a copy of some range of elements that contains no consecutive duplicates
(function template & algorithm function object)
(C++20)
Order-changing operations
Defined in header
reverses the order of elements in a range
(function template & algorithm function object)
(C++20)
creates a copy of a range that is reversed
(function template & algorithm function object)
(C++20)
rotates the order of elements in a range
(function template & algorithm function object)
(C++20)
copies and rotate a range of elements
(function template & algorithm function object)
(C++20)
(C++20)(C++20)
shifts elements in a range
(function template & algorithm function object)
ranges::shift_leftranges::shift_right
(C++23)(C++23)
(until C++17)(C++11)
randomly re-orders elements in a range
(function template & algorithm function object)
(C++20)
Sampling operations
Sorting and related operations
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
is a
pointing to an element of the sequence, comp(*(iter+n),*iter)==false
.
(until C++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
is a valid iterator pointing to an element of the sequence, bool(std::invoke(comp,std::invoke(proj,*(iter+n)),
std::invoke(proj,*iter)))
is false.
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 C++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))
is true if and only if i<n.
↑
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
(C++11)
determines if the range is partitioned by the given predicate
(function template & algorithm function object)
(C++20)
divides a range of elements into two groups
(function template & algorithm function object)
(C++20)
(C++11)
copies a range dividing the elements into two groups
(function template & algorithm function object)
(C++20)
divides elements into two groups while preserving their relative order within each group
(function template & algorithm function object)
(C++20)
(C++11)
locates the partition point of a partitioned range
(function template & algorithm function object)
(C++20)
Sorting operations
Defined in header
sorts a range of elements
(function template & algorithm function object)
(C++20)
sorts a range of elements while preserving relative order between equivalent elements
(function template & algorithm function object)
(C++20)
sorts the first N elements of a range
(function template & algorithm function object)
(C++20)
copies and partially sorts a range of elements
(function template & algorithm function object)
(C++20)
(C++11)
checks whether a range is sorted
(function template & algorithm function object)
(C++20)
(C++11)
finds the largest sorted subrange
(function template & algorithm function object)
(C++20)
finds the Nth element if the range were sorted
(function template & algorithm function object)
(C++20)
Binary search operations (on partitioned ranges)
Defined in header
finds the first element not less than the given value using binary search
(function template & algorithm function object)
(C++20)
finds the first element greater than the given value using binary search
(function template & algorithm function object)
(C++20)
finds the range of elements matching the given value using binary search
(function template & algorithm function object)
(C++20)
determines if an element exists in a range using binary search
(function template & algorithm function object)
(C++20)
Set operations (on sorted ranges)
Defined in header
determines if one sequence is a subsequence of another
(function template & algorithm function object)
(C++20)
computes the union of two sets
(function template & algorithm function object)
(C++20)
computes the intersection of two sets
(function template & algorithm function object)
(C++20)
computes the difference between two sets
(function template & algorithm function object)
(C++20)
computes the symmetric difference between two sets
(function template & algorithm function object)
ranges::set_symmetric_difference
(C++20)
Merge operations (on sorted ranges)
Heap operations
A random access
[first, last) is a heap with respect to a comparator comp if bool(comp(first[(i-1)/2],first[i])) is false for all integer i in (0, last-first).
(until C++20)A random access
[first, last) is a heap with respect to comp and proj for a comparator comp and projection proj if bool(std::invoke(comp,std::invoke(proj,first[(i-1)/2]),
std::invoke(proj,first[i])) is false for all integer i in (0, last-first).
A random access range [first, last) is a heap with respect to a comparator comp if the range is a heap with respect to comp and std::identity{} (the identity projection).
(since C++20)A heap can be created by
and
(since C++20).
For more properties of heap, see
.
Defined in header
adds an element to a max heap
(function template & algorithm function object)
(C++20)
removes the largest element from a max heap
(function template & algorithm function object)
(C++20)
creates a max heap out of a range of elements
(function template & algorithm function object)
(C++20)
turns a max heap into a range of elements sorted in ascending order
(function template & algorithm function object)
(C++20)
(C++11)
checks if the given range is a max heap
(function template & algorithm function object)
(C++20)
(C++11)
finds the largest subrange that is a max heap
(function template & algorithm function object)
(C++20)
Minimum/maximum operations
Defined in header
returns the greater of the given values
(function template & algorithm function object)
(C++20)
returns the largest element in a range
(function template & algorithm function object)
(C++20)
returns the smaller of the given values
(function template & algorithm function object)
(C++20)
returns the smallest element in a range
(function template & algorithm function object)
(C++20)
(C++11)
returns the smaller and larger of two elements
(function template & algorithm function object)
(C++20)
(C++11)
returns the smallest and the largest elements in a range
(function template & algorithm function object)
(C++20)
(C++17)
clamps a value between a pair of boundary values
(function template & algorithm function object)
(C++20)
Lexicographical comparison operations
Permutation operations
Defined in header
generates the next greater lexicographic permutation of a range of elements
(function template & algorithm function object)
(C++20)
generates the next smaller lexicographic permutation of a range of elements
(function template & algorithm function object)
(C++20)
(C++11)
determines if a sequence is a permutation of another sequence
(function template & algorithm function object)
(C++20)
Numeric operations
Defined in header
(C++11)
fills a range with successive increments of the starting value
(function template & algorithm function object)
(C++23)
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)
(C++17)
similar to
, except out of order
(function template)
(C++17)
similar to
, excludes the ith input element from the ith sum
(function template)
(C++17)
similar to
, includes the ith input element in the ith sum
(function template)
(C++17)
applies an invocable, then reduces out of order
(function template)
(C++17)
applies an invocable, then calculates exclusive scan
(function template)
(C++17)
applies an invocable, then calculates inclusive scan
(function template)
Specialized <memory> algorithms
Specialized <random> algorithms (since C++26)
Notes
macro ValueStdFeature
__cpp_lib_algorithm_default_value_type
(C++26)
for algorithms
__cpp_lib_algorithm_iterator_requirements
(C++23)Ranges iterators as inputs to non-Ranges algorithms
(C++17)
__cpp_lib_constexpr_algorithms
(C++20)Constexpr for algorithms
(C++26)Constexpr stable sorting
(C++17)Execution policies
(C++20)
std::execution::unsequenced_policy
__cpp_lib_freestanding_algorithm
(C++26)Freestanding facilities in
(C++17)Parallel algorithms
(C++26)Parallel range algorithms
__cpp_lib_robust_nonmodifying_seq_ops
(C++14)Making non-modifying sequence operations more robust (two-range overloads for
,
and std::is_permutation)
(C++17)
(C++20)std::shift_left and std::shift_rightC library
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 C++ standards.
DR Applied to Behavior as published Correct behavior
C++98 heap required *first to be the largest element there can be elements
equal to *first
C++98 the definition of a sorted sequence was incorrect corrected
C++98 the heap requirement did not match the
definition of
closely enough requirement improved See also