# Algorithms library

< cpp

C++
 Language Headers Freestanding and hosted implementations Named requirements Language support library Concepts library (C++20) Diagnostics library Utilities library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications

Algorithm library
Execution policies (C++17)
Non-modifying sequence operations
 all_ofany_ofnone_of(C++11)(C++11)(C++11) for_each for_each_n(C++17)
Modifying sequence operations
 copycopy_if(C++11) copy_n(C++11) copy_backward move(C++11) move_backward(C++11) shift_leftshift_right(C++20)(C++20) transform
 removeremove_if replacereplace_if reverse rotate unique random_shuffle(until C++17)
Operations on uninitialized storage
 uninitialized_copy_n(C++11) uninitialized_move_n(C++17) uninitialized_fill_n
 destroy(C++17)
 destroy_n(C++17)
Partitioning operations
 is_partitioned(C++11) partition_point(C++11)
 partition partition_copy(C++11)
Sorting operations
 is_sorted(C++11) is_sorted_until(C++11)
Binary search operations
Set operations (on sorted ranges)
Heap operations
 is_heap(C++11) is_heap_until(C++11)
Minimum/maximum operations
 minmax(C++11) minmax_element(C++11)
 clamp(C++17) compare_3way(C++20)
Permutations
 is_permutation(C++11)
Numeric operations
 accumulate reduce(C++17) transform_reduce(C++17)
 partial_sum inclusive_scan(C++17) exclusive_scan(C++17)
 transform_inclusive_scan(C++17) transform_exclusive_scan(C++17)
C 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.

##### Execution policies

Most algorithms have overloads that accept execution policies. The standard library algorithms support three execution policies: sequential, parallel, and parallel+vector, 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: std::execution::seq, std::execution::par, or std::execution::par_unseq.

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_policy(C++17)(C++17)(C++17) execution policy types (class) seqparpar_unseq(C++17)(C++17)(C++17) global execution policy objects (constant) Defined in namespace std is_execution_policy(C++17) test whether a class represents an execution policy (class template)
(since C++17)
##### Non-modifying sequence operations
Defined in header <algorithm>
(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)
applies a function to a range of elements
(function template)
applies a function object to the first n elements of a sequence
(function template)
returns the number of elements satisfying specific criteria
(function template)
finds the first position where two ranges differ
(function template)
finds the first element satisfying specific criteria
(function template)
finds the last sequence of elements in a certain range
(function template)
searches for any one of a set of elements
(function template)
finds the first two adjacent items that are equal (or satisfy a given predicate)
(function template)
searches for a range of elements
(function template)
searches a range for a number of consecutive copies of an element
(function template)
##### Modifying sequence operations
Defined in header <algorithm>
copies a range of elements to a new location
(function template)
(C++11)
copies a number of elements to a new location
(function template)
copies a range of elements in backwards order
(function template)
(C++11)
moves a range of elements to a new location
(function template)
moves a range of elements to a new location in backwards order
(function template)
copy-assigns the given value to every element in a range
(function template)
copy-assigns the given value to N elements in a range
(function template)
applies a function to a range of elements
(function template)
assigns the results of successive function calls to every element in a range
(function template)
assigns the results of successive function calls to N elements in a range
(function template)
removes elements satisfying specific criteria
(function template)
copies a range of elements omitting those that satisfy specific criteria
(function template)
replaces all values satisfying specific criteria with another value
(function template)
copies a range, replacing elements satisfying specific criteria with another value
(function template)
swaps the values of two objects
(function template)
swaps two ranges of elements
(function template)
swaps the elements pointed to by two iterators
(function template)
reverses the order of elements in a range
(function template)
creates a copy of a range that is reversed
(function template)
rotates the order of elements in a range
(function template)
copies and rotate a range of elements
(function template)
shifts elements in a range
(function template)
(until C++17)(C++11)
randomly re-orders elements in a range
(function template)
(C++17)
selects n random elements from a sequence
(function template)
removes consecutive duplicate elements in a range
(function template)
creates a copy of some range of elements that contains no consecutive duplicates
(function template)
##### Partitioning operations
Defined in header <algorithm>
determines if the range is partitioned by the given predicate
(function template)
divides a range of elements into two groups
(function template)
copies a range dividing the elements into two groups
(function template)
divides elements into two groups while preserving their relative order
(function template)
locates the partition point of a partitioned range
(function template)
##### Sorting operations
Defined in header <algorithm>
(C++11)
checks whether a range is sorted into ascending order
(function template)
finds the largest sorted subrange
(function template)
sorts a range into ascending order
(function template)
sorts the first N elements of a range
(function template)
copies and partially sorts a range of elements
(function template)
sorts a range of elements while preserving order between equal elements
(function template)
partially sorts the given range making sure that it is partitioned by the given element
(function template)
##### Binary search operations (on sorted ranges)
Defined in header <algorithm>
returns an iterator to the first element not less than the given value
(function template)
returns an iterator to the first element greater than a certain value
(function template)
determines if an element exists in a certain range
(function template)
returns range of elements matching a specific key
(function template)
##### Other operations on sorted ranges
Defined in header <algorithm>
merges two sorted ranges
(function template)
merges two ordered ranges in-place
(function template)
##### Set operations (on sorted ranges)
Defined in header <algorithm>
returns true if one set is a subset of another
(function template)
computes the difference between two sets
(function template)
computes the intersection of two sets
(function template)
computes the symmetric difference between two sets
(function template)
computes the union of two sets
(function template)
##### Heap operations
Defined in header <algorithm>
(C++11)
checks if the given range is a max heap
(function template)
finds the largest subrange that is a max heap
(function template)
creates a max heap out of a range of elements
(function template)
adds an element to a max heap
(function template)
removes the largest element from a max heap
(function template)
turns a max heap into a range of elements sorted in ascending order
(function template)
##### Minimum/maximum operations
Defined in header <algorithm>
returns the greater of the given values
(function template)
returns the largest element in a range
(function template)
returns the smaller of the given values
(function template)
returns the smallest element in a range
(function template)
(C++11)
returns the smaller and larger of two elements
(function template)
returns the smallest and the largest elements in a range
(function template)
(C++17)
clamps a value between a pair of boundary values
(function template)
##### Comparison operations
Defined in header <algorithm>
determines if two sets of elements are the same
(function template)
returns true if one range is lexicographically less than another
(function template)
compares two values using three-way comparison
(function template)
compares two ranges using three-way comparison
(function template)
##### Permutation operations
Defined in header <algorithm>
determines if a sequence is a permutation of another sequence
(function template)
generates the next greater lexicographic permutation of a range of elements
(function template)
generates the next smaller lexicographic permutation of a range of elements
(function template)
##### Numeric operations
Defined in header <numeric>
(C++11)
fills a range with successive increments of the starting value
(function template)
sums up 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 std::accumulate, except out of order
(function template)
similar to std::partial_sum, excludes the ith input element from the ith sum
(function template)
similar to std::partial_sum, includes the ith input element in the ith sum
(function template)
applies a functor, then reduces out of order
(function template)
applies a functor, then calculates exclusive scan
(function template)
applies a functor, 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)
copies a number of objects to an uninitialized area of memory
(function template)
copies an object to an uninitialized area of memory, defined by a range
(function template)
copies an object to an uninitialized area of memory, defined by a start and a count
(function template)
moves a range of objects to an uninitialized area of memory
(function template)
moves a number of objects to an uninitialized area of memory
(function template)
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 start and a count
(function template)
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 start and a count
(function template)
destroys an object at a given address
(function template)
(C++17)
destroys a range of objects
(function template)
(C++17)
destroys a number of objects in a range
(function template)
##### 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)