##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Algorithms library

From cppreference.com
< cpp

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 `<execution>`
Defined in namespace `execution`
sequenced_policy parallel_policy parallel_unsequenced_policy (C++17)(C++17)(C++17) | execution policy types (class) [edit]
seq par par_unseq (C++17)(C++17)(C++17) | global execution policy objects (constant) [edit]
is_execution_policy (C++17) | test whether a class represents an execution policy (class template) [edit]
| (since C++17)

##### Non-modifying sequence operations
Defined in header `<algorithm>`
all_of any_of none_of (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) [edit]
for_each | applies a function to a range of elements (function template) [edit]
for_each_n (C++17) | applies a function object to the first n elements of a sequence (function template) [edit]
count count_if | returns the number of elements satisfying specific criteria (function template) [edit]
mismatch | finds the first position where two ranges differ (function template) [edit]
equal | determines if two sets of elements are the same (function template) [edit]
find find_if find_if_not (C++11) | finds the first element satisfying specific criteria (function template) [edit]
find_end | finds the last sequence of elements in a certain range (function template) [edit]
find_first_of | searches for any one of a set of elements (function template) [edit]
adjacent_find | finds the first two adjacent items that are equal (or satisfy a given predicate) (function template) [edit]
search | searches for a range of elements (function template) [edit]
search_n | searches for a number consecutive copies of an element in a range (function template) [edit]
##### Modifying sequence operations
Defined in header `<algorithm>`
copy copy_if (C++11) | copies a range of elements to a new location (function template) [edit]
copy_n (C++11) | copies a number of elements to a new location (function template) [edit]
copy_backward | copies a range of elements in backwards order (function template) [edit]
move (C++11) | moves a range of elements to a new location (function template) [edit]
move_backward (C++11) | moves a range of elements to a new location in backwards order (function template) [edit]
fill | copy-assigns the given value to every element in a range (function template) [edit]
fill_n | copy-assigns the given value to N elements in a range (function template) [edit]
transform | applies a function to a range of elements (function template) [edit]
generate | assigns the results of successive function calls to every element in a range (function template) [edit]
generate_n | assigns the results of successive function calls to N elements in a range (function template) [edit]
remove remove_if | removes elements satisfying specific criteria (function template) [edit]
remove_copy remove_copy_if | copies a range of elements omitting those that satisfy specific criteria (function template) [edit]
replace replace_if | replaces all values satisfying specific criteria with another value (function template) [edit]
replace_copy replace_copy_if | copies a range, replacing elements satisfying specific criteria with another value (function template) [edit]
swap | swaps the values of two objects (function template) [edit]
swap_ranges | swaps two ranges of elements (function template) [edit]
iter_swap | swaps the elements pointed to by two iterators (function template) [edit]
reverse | reverses the order of elements in a range (function template) [edit]
reverse_copy | creates a copy of a range that is reversed (function template) [edit]
rotate | rotates the order of elements in a range (function template) [edit]
rotate_copy | copies and rotate a range of elements (function template) [edit]
random_shuffle shuffle (until C++17)(C++11) | randomly re-orders elements in a range (function template) [edit]
sample (C++17) | selects n random elements from a sequence (function template) [edit]
unique | removes consecutive duplicate elements in a range (function template) [edit]
unique_copy | creates a copy of some range of elements that contains no consecutive duplicates (function template) [edit]
##### Partitioning operations
Defined in header `<algorithm>`
is_partitioned (C++11) | determines if the range is partitioned by the given predicate (function template) [edit]
partition | divides a range of elements into two groups (function template) [edit]
partition_copy (C++11) | copies a range dividing the elements into two groups (function template) [edit]
stable_partition | divides elements into two groups while preserving their relative order (function template) [edit]
partition_point (C++11) | locates the partition point of a partitioned range (function template) [edit]
##### Sorting operations
Defined in header `<algorithm>`
is_sorted (C++11) | checks whether a range is sorted into ascending order (function template) [edit]
is_sorted_until (C++11) | finds the largest sorted subrange (function template) [edit]
sort | sorts a range into ascending order (function template) [edit]
partial_sort | sorts the first N elements of a range (function template) [edit]
partial_sort_copy | copies and partially sorts a range of elements (function template) [edit]
stable_sort | sorts a range of elements while preserving order between equal elements (function template) [edit]
nth_element | partially sorts the given range making sure that it is partitioned by the given element (function template) [edit]
##### Binary search operations (on sorted ranges)
Defined in header `<algorithm>`
lower_bound | returns an iterator to the first element not less than the given value (function template) [edit]
upper_bound | returns an iterator to the first element greater than a certain value (function template) [edit]
binary_search | determines if an element exists in a certain range (function template) [edit]
equal_range | returns range of elements matching a specific key (function template) [edit]
##### Set operations (on sorted ranges)
Defined in header `<algorithm>`
merge | merges two sorted ranges (function template) [edit]
inplace_merge | merges two ordered ranges in-place (function template) [edit]
includes | returns true if one set is a subset of another (function template) [edit]
set_difference | computes the difference between two sets (function template) [edit]
set_intersection | computes the intersection of two sets (function template) [edit]
set_symmetric_difference | computes the symmetric difference between two sets (function template) [edit]
set_union | computes the union of two sets (function template) [edit]
##### Heap operations
Defined in header `<algorithm>`
is_heap (C++11) | checks if the given range is a max heap (function template) [edit]
is_heap_until (C++11) | finds the largest subrange that is a max heap (function template) [edit]
make_heap | creates a max heap out of a range of elements (function template) [edit]
push_heap | adds an element to a max heap (function template) [edit]
pop_heap | removes the largest element from a max heap (function template) [edit]
sort_heap | turns a max heap into a range of elements sorted in ascending order (function template) [edit]
##### Minimum/maximum operations
Defined in header `<algorithm>`
max | returns the greater of the given values (function template) [edit]
max_element | returns the largest element in a range (function template) [edit]
min | returns the smaller of the given values (function template) [edit]
min_element | returns the smallest element in a range (function template) [edit]
minmax (C++11) | returns the smaller and larger of two elements (function template) [edit]
minmax_element (C++11) | returns the smallest and the largest elements in a range (function template) [edit]
clamp (C++17) | clamps a value between a pair of boundary values (function template) [edit]
lexicographical_compare | returns true if one range is lexicographically less than another (function template) [edit]
is_permutation (C++11) | determines if a sequence is a permutation of another sequence (function template) [edit]
next_permutation | generates the next greater lexicographic permutation of a range of elements (function template) [edit]
prev_permutation | generates the next smaller lexicographic permutation of a range of elements (function template) [edit]
##### Numeric operations
Defined in header `<numeric>`
iota (C++11) | fills a range with successive increments of the starting value (function template) [edit]
accumulate | sums up a range of elements (function template) [edit]
inner_product | computes the inner product of two ranges of elements (function template) [edit]
adjacent_difference | computes the differences between adjacent elements in a range (function template) [edit]
partial_sum | computes the partial sum of a range of elements (function template) [edit]
reduce (C++17) | similar to std::accumulate, except out of order (function template) [edit]
exclusive_scan (C++17) | similar to std::partial_sum, excludes the ith input element from the ith sum (function template) [edit]
inclusive_scan (C++17) | similar to std::partial_sum, includes the ith input element in the ith sum (function template) [edit]
transform_reduce (C++17) | applies a functor, then reduces out of order (function template) [edit]
transform_exclusive_scan (C++17) | applies a functor, then calculates exclusive scan (function template) [edit]
transform_inclusive_scan (C++17) | applies a functor, then calculates inclusive scan (function template) [edit]
##### Operations on uninitialized memory

Defined in header `<memory>`
uninitialized_copy | copies a range of objects to an uninitialized area of memory (function template) [edit]
uninitialized_copy_n (C++11) | copies a number of objects to an uninitialized area of memory (function template) [edit]
uninitialized_fill | copies an object to an uninitialized area of memory, defined by a range (function template) [edit]
uninitialized_fill_n | copies an object to an uninitialized area of memory, defined by a start and a count (function template) [edit]
uninitialized_move (C++17) | moves a range of objects to an uninitialized area of memory (function template) [edit]
uninitialized_move_n (C++17) | moves a number of objects to an uninitialized area of memory (function template) [edit]
uninitialized_default_construct (C++17) | constructs objects by default-initialization in an uninitialized area of memory, defined by a range (function template) [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]
uninitialized_value_construct (C++17) | constructs objects by value-initialization in an uninitialized area of memory, defined by a range (function template) [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]
destroy_at (C++17) | destroys an object at a given address (function template) [edit]
destroy (C++17) | destroys a range of objects (function template) [edit]
destroy_n (C++17) | destroys a number of objects in a range (function template) [edit]
##### C library
Defined in header `<cstdlib>`
qsort | sorts a range of elements with unspecified type (function) [edit]
bsearch | searches an array for an element of unspecified type (function) [edit]

### [edit] See also

C documentation for Algorithms

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/algorithm&oldid=89874"