Algorithms library

From cppreference.com
< cpp
 
 
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
Constrained numeric operations
Fold operations
Operations on uninitialized storage
Return types
 

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

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.

std::vector<int> v = {7, 1, 4, 0, -1};
std::ranges::sort(v); // constrained algorithm
(since C++20)


Execution policies

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.

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

See also