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.
Contents
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.
[edit ] Execution policies (since C++17)Most 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 T
is the type of elements.
[edit ] Non-modifying sequence operations [edit ] Batch operations [edit ] Search operations
[edit ] Fold operations (since C++23)
[edit ] Modifying sequence operations [edit ] Copy operations
Defined in header
copycopy_if (C++11)
copies a range of elements to a new location (function template) [edit]
ranges::copyranges::copy_if (C++20)(C++20)
copies a range of elements to a new location(algorithm function object)[edit]
copy_n (C++11)
copies a number of elements to a new location (function template) [edit]
ranges::copy_n (C++20)
copies a number of elements to a new location(algorithm function object)[edit]
copy_backward
copies a range of elements in backwards order (function template) [edit]
ranges::copy_backward (C++20)
copies a range of elements in backwards order(algorithm function object)[edit]
move (C++11)
moves a range of elements to a new location (function template) [edit]
ranges::move (C++20)
moves a range of elements to a new location(algorithm function object)[edit]
move_backward (C++11)
moves a range of elements to a new location in backwards order (function template) [edit]
ranges::move_backward (C++20)
moves a range of elements to a new location in backwards order(algorithm function object)[edit]
[edit ] Swap operations
Defined in header (until C++11)
Defined in header (since C++11)
Defined in header <string_view>
swap
swaps the values of two objects (function template) [edit]
Defined in header
swap_ranges
swaps two ranges of elements (function template) [edit]
ranges::swap_ranges (C++20)
swaps two ranges of elements(algorithm function object)[edit]
iter_swap
swaps the elements pointed to by two iterators (function template) [edit]
[edit ] Generation operations
Defined in header
fill
copy-assigns the given value to every element in a range (function template) [edit]
ranges::fill (C++20)
assigns a range of elements a certain value(algorithm function object)[edit]
fill_n
copy-assigns the given value to N elements in a range (function template) [edit]
ranges::fill_n (C++20)
assigns a value to a number of elements(algorithm function object)[edit]
generate
assigns the results of successive function calls to every element in a range (function template) [edit]
ranges::generate (C++20)
saves the result of a function in a range(algorithm function object)[edit]
generate_n
assigns the results of successive function calls to N elements in a range (function template) [edit]
ranges::generate_n (C++20)
saves the result of N applications of a function(algorithm function object)[edit]
[edit ] Removing operations
[edit ] Order-changing operations
[edit ] Sampling operations [edit ] RequirementsSome 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 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[1] is a valid iterator pointing to an element of the sequence, bool(std::invoke (comp, std::invoke (proj, *(iter + n)), std::invoke (proj, *iter)))[1] 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))[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.
[edit ] Partitioning operations
[edit ] Sorting operations
[edit ] Binary search operations (on partitioned ranges)
Defined in header
lower_bound
returns an iterator to the first element not less than the given value (function template) [edit]
ranges::lower_bound (C++20)
returns an iterator to the first element not less than the given value(algorithm function object)[edit]
upper_bound
returns an iterator to the first element greater than a certain value (function template) [edit]
ranges::upper_bound (C++20)
returns an iterator to the first element greater than a certain value(algorithm function object)[edit]
equal_range
returns range of elements matching a specific key (function template) [edit]
ranges::equal_range (C++20)
returns range of elements matching a specific key(algorithm function object)[edit]
binary_search
determines if an element exists in a partially-ordered range (function template) [edit]
ranges::binary_search (C++20)
determines if an element exists in a partially-ordered range(algorithm function object)[edit]
[edit ] Set operations (on sorted ranges)
[edit ] Merge operations (on sorted ranges) [edit ] Heap operations
A random access range [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 range [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 std::make_heap and ranges::make_heap (since C++20).
For more properties of heap, see max heap .
Defined in header
push_heap
adds an element to a max heap (function template) [edit]
ranges::push_heap (C++20)
adds an element to a max heap(algorithm function object)[edit]
pop_heap
removes the largest element from a max heap (function template) [edit]
ranges::pop_heap (C++20)
removes the largest element from a max heap(algorithm function object)[edit]
make_heap
creates a max heap out of a range of elements (function template) [edit]
ranges::make_heap (C++20)
creates a max heap out of a range of elements(algorithm function object)[edit]
sort_heap
turns a max heap into a range of elements sorted in ascending order (function template) [edit]
ranges::sort_heap (C++20)
turns a max heap into a range of elements sorted in ascending order(algorithm function object)[edit]
is_heap (C++11)
checks if the given range is a max heap (function template) [edit]
ranges::is_heap (C++20)
checks if the given range is a max heap(algorithm function object)[edit]
is_heap_until (C++11)
finds the largest subrange that is a max heap (function template) [edit]
ranges::is_heap_until (C++20)
finds the largest subrange that is a max heap(algorithm function object)[edit]
[edit ] Minimum/maximum operations
Defined in header
max
returns the greater of the given values (function template) [edit]
ranges::max (C++20)
returns the greater of the given values(algorithm function object)[edit]
max_element
returns the largest element in a range (function template) [edit]
ranges::max_element (C++20)
returns the largest element in a range(algorithm function object)[edit]
min
returns the smaller of the given values (function template) [edit]
ranges::min (C++20)
returns the smaller of the given values(algorithm function object)[edit]
min_element
returns the smallest element in a range (function template) [edit]
ranges::min_element (C++20)
returns the smallest element in a range(algorithm function object)[edit]
minmax (C++11)
returns the smaller and larger of two elements (function template) [edit]
ranges::minmax (C++20)
returns the smaller and larger of two elements(algorithm function object)[edit]
minmax_element (C++11)
returns the smallest and the largest elements in a range (function template) [edit]
ranges::minmax_element (C++20)
returns the smallest and the largest elements in a range(algorithm function object)[edit]
clamp (C++17)
clamps a value between a pair of boundary values (function template) [edit]
ranges::clamp (C++20)
clamps a value between a pair of boundary values(algorithm function object)[edit]
[edit ] Lexicographical comparison operations [edit ] Permutation operations
Defined in header
next_permutation
generates the next greater lexicographic permutation of a range of elements (function template) [edit]
ranges::next_permutation (C++20)
generates the next greater lexicographic permutation of a range of elements(algorithm function object)[edit]
prev_permutation
generates the next smaller lexicographic permutation of a range of elements (function template) [edit]
ranges::prev_permutation (C++20)
generates the next smaller lexicographic permutation of a range of elements(algorithm function object)[edit]
is_permutation (C++11)
determines if a sequence is a permutation of another sequence (function template) [edit]
ranges::is_permutation (C++20)
determines if a sequence is a permutation of another sequence(algorithm function object)[edit]
[edit ] Numeric operations
[edit ] Operations on uninitialized memory
Defined in header
uninitialized_copy
copies a range of objects to an uninitialized area of memory (function template) [edit]
ranges::uninitialized_copy (C++20)
copies a range of objects to an uninitialized area of memory(algorithm function object)[edit]
uninitialized_copy_n (C++11)
copies a number of objects to an uninitialized area of memory (function template) [edit]
ranges::uninitialized_copy_n (C++20)
copies a number of objects to an uninitialized area of memory(algorithm function object)[edit]
uninitialized_fill
copies an object to an uninitialized area of memory, defined by a range (function template) [edit]
ranges::uninitialized_fill (C++20)
copies an object to an uninitialized area of memory, defined by a range(algorithm function object)[edit]
uninitialized_fill_n
copies an object to an uninitialized area of memory, defined by a start and a count (function template) [edit]
ranges::uninitialized_fill_n (C++20)
copies an object to an uninitialized area of memory, defined by a start and a count(algorithm function object)[edit]
uninitialized_move (C++17)
moves a range of objects to an uninitialized area of memory (function template) [edit]
ranges::uninitialized_move (C++20)
moves a range of objects to an uninitialized area of memory(algorithm function object)[edit]
uninitialized_move_n (C++17)
moves a number of objects to an uninitialized area of memory (function template) [edit]
ranges::uninitialized_move_n (C++20)
moves a number of objects to an uninitialized area of memory(algorithm function object)[edit]
uninitialized_default_construct (C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range (function template) [edit]
ranges::uninitialized_default_construct (C++20)
constructs objects by default-initialization in an uninitialized area of memory, defined by a range(algorithm function object)[edit]
uninitialized_default_construct_n (C++17)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count (function template) [edit]
ranges::uninitialized_default_construct_n (C++20)
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and count(algorithm function object)[edit]
uninitialized_value_construct (C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range (function template) [edit]
ranges::uninitialized_value_construct (C++20)
constructs objects by value-initialization in an uninitialized area of memory, defined by a range(algorithm function object)[edit]
uninitialized_value_construct_n (C++17)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count (function template) [edit]
ranges::uninitialized_value_construct_n (C++20)
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count(algorithm function object)[edit]
destroy (C++17)
destroys a range of objects (function template) [edit]
ranges::destroy (C++20)
destroys a range of objects(algorithm function object)[edit]
destroy_n (C++17)
destroys a number of objects in a range (function template) [edit]
ranges::destroy_n (C++20)
destroys a number of objects in a range(algorithm function object)[edit]
destroy_at (C++17)
destroys an object at a given address (function template) [edit]
ranges::destroy_at (C++20)
destroys an object at a given address(algorithm function object)[edit]
construct_at (C++20)
creates an object at a given address (function template) [edit]
ranges::construct_at (C++20)
creates an object at a given address(algorithm function object)[edit]
[edit ] Random number generation (since C++26)
[edit ] C library
[edit ] Defect reportsThe following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR
Applied to
Behavior as published
Correct behavior
LWG 193
C++98
heap required *first to be the largest element
there can be elementsequal to *first
LWG 2150
C++98
the definition of a sorted sequence was incorrect
corrected
LWG 2166
C++98
the heap requirement did not match thedefinition of max heap closely enough
requirement improved
[edit ] See also