C++ Standard Library C++ STL Library

C++ - <algorithm>



C++ <algorithm>

The C++ <algorithm> header defines a number of functions which is designed especially to use with ranges of elements. A range is a sequence of elements which can be accessed using an iterator or a pointer, for example - vector, list or array.

C++ - Algorithm Functions

The C++ <algorithm> header defines a number of functions which are listed below:

Non-modifying sequence operations

FunctionsDescription
all_of() Test if all elements in range satisfy specified condition.
any_of() Test if any element in range satisfy specified condition.
none_of() Test if no element in range satisfy specified condition.
for_each() Apply specified function to range.
find() Find value in range.
find_if() Find element in range based on specified condition.
find_if_not() Find element in range based on specified condition (negative condition).
find_end() Find last subsequence in range
find_first_of() Find element from set in range
adjacent_find() Find equal adjacent elements in range
count() Returns number of occurrences of specified value in range.
count_if() Return number of elements in range satisfying specified condition.
mismatch() Return first position where two ranges differ
equal() Check if the elements in two ranges are equal.
is_permutation() Test whether range is permutation of another
search() Search a given range for a sequence.
search_n() Search a given range for elements

Modifying sequence operations

FunctionsDescription
copy() Copy range of elements.
copy_n() Copy sequence of elements.
copy_if() Copy elements of range based on specified condition.
copy_backward() Copy range of elements backward.
move() Move range of elements
move_backward() Move range of elements backward
swap() Exchange values of two objects
swap_ranges() Exchange values of two ranges
iter_swap() Exchange values of objects pointed to by two iterators
transform() Transform range
replace() Replace old value with a new value in range.
replace_if() Replace values in range based on specified condition.
replace_copy() Copy range and replace old value with a new value.
replace_copy_if() Copy range and replace value based on specified condition.
fill() Fill range with specified value.
fill_n() Fill sequence with specified value.
generate() Generate values for range using specified function.
generate_n() Generate values for sequence with specified function.
remove() Remove specified value from range.
remove_if() Remove elements from range based on specified condition.
remove_copy() Copy range removing value
remove_copy_if() Copy range removing values
unique() Remove consecutive duplicates in range
unique_copy() Copy range removing duplicates
reverse() Reverse the order of elements in range.
reverse_copy() Copy and reverse the order of elements in range.
rotate() Rotate left the elements in range
rotate_copy() Copy range rotated left
random_shuffle() Randomly shuffle elements in the range.
shuffle() Randomly shuffle elements in the range using given generator.

Partitions

FunctionsDescription
is_partitioned() Test whether range is partitioned
partition() Partition range in two
stable_partition() Partition range in two - stable ordering
partition_copy() Partition range into two
partition_point() Get partition point

Sorting

FunctionsDescription
sort() Sort elements in the given range.
stable_sort() Sort elements in the given range and preserving order of equivalents.
partial_sort() Partially sort elements in range
partial_sort_copy() Copy and partially sort range
is_sorted() Check whether the given range is sorted.
is_sorted_until() Find first unsorted element in range
nth_element() Sort element in range

Binary search (operating on partitioned/sorted ranges)

FunctionsDescription
lower_bound() Return iterator to lower bound.
upper_bound() Return iterator to upper bound.
equal_range() Get subrange of equal elements.
binary_search() Check if the value exists in the sorted sequence.

Merge (operating on sorted ranges)

FunctionsDescription
merge() Merge two sorted ranges.
inplace_merge() Merge consecutive sorted ranges
includes() Check if sorted range includes another sorted range.
set_union() Union of two sorted ranges.
set_intersection() Intersection of two sorted ranges.
set_difference() Difference of two sorted ranges.
set_symmetric_difference() Symmetric difference of two sorted ranges.

Heap

FunctionsDescription
make_heap() Make heap from range.
pop_heap() Delete element from heap range.
push_heap() Push element into heap range.
sort_heap() Sorts elements of heap.
is_heap() Test if range is heap.
is_heap_until() Find first element not in heap order

Min/Max

FunctionsDescription
min() Return smallest element.
max() Return largest element.
minmax() Return smallest and largest elements.
min_element() Return smallest element in the range.
max_element() Return largest element in the range.
minmax_element() Return smallest and largest elements in the range.

Other

FunctionsDescription
lexicographical_compare() Lexicographical less-than comparison
next_permutation() Transform range to next permutation
prev_permutation() Transform range to previous permutation