C++ Algorithm Functions

About This Index
This page is the C++ Algorithm Functions index. Each row links to a full guide with syntax, three examples with output, and FAQs. All of them live in the <algorithm> header.
Learn what an algorithm is on /cpp/algorithm. Use this page when you need a specific function such as sort(), find(), or count_if().
Start with sort(), find(), count(), count_if(), and reverse(). Then explore the sorted-range and heap tools.
How to Call an Algorithm
Most algorithms take two iterators, first and last. They mark a range that starts at first and stops just before last:
#include <algorithm>
#include <vector>
std::vector<int> v = {5, 2, 9, 1};
std::sort(v.begin(), v.end()); // sort the whole vector
auto it = std::find(v.begin(), v.end(), 9); // returns an iterator
int n = std::count(v.begin(), v.end(), 2); // returns a number
bool ok = std::any_of(v.begin(), v.end(),
[](int x) { return x > 8; }); // condition as a lambda Choose a Function by Job
Know what you want to do but not the name? Start here.
C++ Algorithm Functions Table
91 tutorials. Search by name or scroll the categories. Each row opens a full guide.
Jump to: Search & check (non-modifying) · Count & visit · Copy, move & swap (modifying) · Fill, generate, transform & replace (modifying) · Remove & unique (modifying) · Reverse, rotate, shift & shuffle (modifying) · Sorting · Binary search (sorted ranges) · Merge & set operations (sorted ranges) · Heap · Min & max · Partition · Permutation
Search & check (non-modifying)
15 functionsLook at the data without changing it: test conditions, find values, and compare ranges.
| Function | Description | Since | Tutorial |
|---|---|---|---|
all_of() | Checks whether every element in a range matches a condition. | C++11 | Try it |
any_of() | Checks whether at least one element in a range matches a condition. | C++11 | Try it |
none_of() | Checks that no element in a range matches a condition. | C++11 | Try it |
find() | Finds the first element in a range that equals a value. | C++98 | Try it |
find_if() | Finds the first element that satisfies a condition. | C++98 | Try it |
find_if_not() | Finds the first element that does not satisfy a condition. | C++11 | Try it |
find_end() | Finds the last occurrence of a sequence inside a range. | C++98 | Try it |
find_first_of() | Finds the first element that matches any value from a second range. | C++98 | Try it |
adjacent_find() | Finds the first two neighbouring elements that are equal. | C++98 | Try it |
search() | Finds the first occurrence of a sequence inside a range. | C++98 | Try it |
search_n() | Finds the first run of n equal elements in a row. | C++98 | Try it |
mismatch() | Finds the first position where two ranges differ. | C++98 | Try it |
equal() | Checks whether two ranges hold the same elements in the same order. | C++98 | Try it |
lexicographical_compare() | Checks whether one range comes before another in dictionary order. | C++98 | Try it |
lexicographical_compare_three_way() | Compares two ranges in dictionary order and returns less, equal, or greater. | C++20 | Try it |
Count & visit
4 functionsCount matching elements or run a function on each element.
| Function | Description | Since | Tutorial |
|---|---|---|---|
count() | Counts how many elements equal a given value. | C++98 | Try it |
count_if() | Counts how many elements satisfy a condition. | C++98 | Try it |
for_each() | Calls a function on every element in a range. | C++98 | Try it |
for_each_n() | Calls a function on the first n elements of a range. | C++17 | Try it |
Copy, move & swap (modifying)
9 functionsCopy or move elements to another place, or exchange values.
| Function | Description | Since | Tutorial |
|---|---|---|---|
copy() | Copies the elements of one range into another place. | C++98 | Try it |
copy_backward() | Copies a range into a destination, working from the last element to the first. | C++98 | Try it |
copy_if() | Copies only the elements that satisfy a condition. | C++11 | Try it |
copy_n() | Copies a fixed number of elements to another place. | C++11 | Try it |
move() | Moves the elements of a range into another place instead of copying them. | C++11 | Try it |
move_backward() | Moves a range into a destination, starting from the last element. | C++11 | Try it |
swap() | Swaps the values of two objects. | C++98 | Try it |
swap_ranges() | Swaps the elements of two ranges one by one. | C++98 | Try it |
iter_swap() | Swaps the values that two iterators point to. | C++98 | Try it |
Fill, generate, transform & replace (modifying)
9 functionsWrite new values into a range, or change values that already exist.
| Function | Description | Since | Tutorial |
|---|---|---|---|
fill() | Assigns the same value to every element in a range. | C++98 | Try it |
fill_n() | Assigns a value to the first n elements of a range. | C++98 | Try it |
generate() | Fills a range with values made by calling a function. | C++98 | Try it |
generate_n() | Fills n elements with values made by calling a function. | C++98 | Try it |
transform() | Applies a function to each element and stores the results. | C++98 | Try it |
replace() | Replaces every element equal to one value with another value. | C++98 | Try it |
replace_if() | Replaces every element that satisfies a condition. | C++98 | Try it |
replace_copy() | Copies a range while replacing one value with another. | C++98 | Try it |
replace_copy_if() | Copies a range while replacing the elements that satisfy a condition. | C++98 | Try it |
Remove & unique (modifying)
6 functionsDrop unwanted elements and collapse repeated neighbours. Pair them with erase().
| Function | Description | Since | Tutorial |
|---|---|---|---|
remove() | Moves all elements not equal to a value to the front and returns the new end. | C++98 | Try it |
remove_if() | Moves elements that do not satisfy a condition to the front and returns the new end. | C++98 | Try it |
remove_copy() | Copies a range while leaving out elements equal to a value. | C++98 | Try it |
remove_copy_if() | Copies a range while leaving out elements that satisfy a condition. | C++98 | Try it |
unique() | Removes consecutive duplicate elements and returns the new end. | C++98 | Try it |
unique_copy() | Copies a range while collapsing consecutive duplicate elements. | C++98 | Try it |
Reverse, rotate, shift & shuffle (modifying)
9 functionsChange the order of elements without sorting them.
| Function | Description | Since | Tutorial |
|---|---|---|---|
reverse() | Reverses the order of the elements in a range. | C++98 | Try it |
reverse_copy() | Copies a range into a destination in reverse order. | C++98 | Try it |
rotate() | Rotates the elements of a range so a chosen element becomes the first. | C++98 | Try it |
rotate_copy() | Copies a range into a destination as if it was rotated. | C++98 | Try it |
shift_left() | Moves the elements of a range toward the front by n places. | C++20 | Try it |
shift_right() | Moves the elements of a range toward the back by n places. | C++20 | Try it |
shuffle() | Randomly reorders the elements of a range using a random engine. | C++11 | Try it |
random_shuffle() | Randomly reorders a range using rand() (removed in C++17). | C++98 | Try it |
sample() | Picks n random elements from a range. | C++17 | Try it |
Sorting
7 functionsSort a whole range, only the first part, or check if it is already sorted.
| Function | Description | Since | Tutorial |
|---|---|---|---|
sort() | Sorts the elements of a range into ascending order. | C++98 | Try it |
stable_sort() | Sorts a range and keeps the order of equal elements. | C++98 | Try it |
partial_sort() | Sorts only the smallest elements to the front of a range. | C++98 | Try it |
partial_sort_copy() | Copies the smallest elements of a range into a sorted destination. | C++98 | Try it |
nth_element() | Puts the element that would be at position n in a sorted range into place. | C++98 | Try it |
is_sorted() | Checks whether a range is sorted. | C++11 | Try it |
is_sorted_until() | Finds the first element that breaks the sorted order. | C++11 | Try it |
Binary search (sorted ranges)
4 functionsVery fast search. The data must be sorted first.
| Function | Description | Since | Tutorial |
|---|---|---|---|
binary_search() | Checks whether a value exists in a sorted range. | C++98 | Try it |
lower_bound() | Finds the first element that is not less than a value in a sorted range. | C++98 | Try it |
upper_bound() | Finds the first element that is greater than a value in a sorted range. | C++98 | Try it |
equal_range() | Finds the range of elements equal to a value in a sorted range. | C++98 | Try it |
Merge & set operations (sorted ranges)
7 functionsCombine two sorted ranges, or treat them like mathematical sets.
| Function | Description | Since | Tutorial |
|---|---|---|---|
merge() | Merges two sorted ranges into one sorted range. | C++98 | Try it |
inplace_merge() | Merges two consecutive sorted parts of one range in place. | C++98 | Try it |
includes() | Checks whether a sorted range contains all elements of another sorted range. | C++98 | Try it |
set_union() | Builds the union of two sorted ranges. | C++98 | Try it |
set_intersection() | Builds the intersection of two sorted ranges. | C++98 | Try it |
set_difference() | Builds the difference of two sorted ranges: elements in the first but not the second. | C++98 | Try it |
set_symmetric_difference() | Builds the symmetric difference of two sorted ranges: elements in exactly one of them. | C++98 | Try it |
Heap
6 functionsKeep the largest (or smallest) element at the front of a vector.
| Function | Description | Since | Tutorial |
|---|---|---|---|
make_heap() | Rearranges a range into a heap. | C++98 | Try it |
push_heap() | Adds the last element of a range into an existing heap. | C++98 | Try it |
pop_heap() | Moves the top element of a heap to the end of the range. | C++98 | Try it |
sort_heap() | Turns a heap into a sorted range. | C++98 | Try it |
is_heap() | Checks whether a range is a heap. | C++11 | Try it |
is_heap_until() | Finds the first element that breaks the heap rule. | C++11 | Try it |
Min & max
7 functionsFind the smallest or largest value, or limit a value to a range.
| Function | Description | Since | Tutorial |
|---|---|---|---|
min() | Returns the smaller of two values, or the smallest in a list. | C++98 | Try it |
max() | Returns the larger of two values, or the largest in a list. | C++98 | Try it |
minmax() | Returns the smaller and the larger of two values (or of a list) at once. | C++11 | Try it |
min_element() | Finds the smallest element in a range. | C++98 | Try it |
max_element() | Finds the largest element in a range. | C++98 | Try it |
minmax_element() | Finds the smallest and the largest elements of a range in one pass. | C++11 | Try it |
clamp() | Limits a value so it stays between a low and a high bound. | C++17 | Try it |
Partition
5 functionsSplit a range into two groups by a condition.
| Function | Description | Since | Tutorial |
|---|---|---|---|
partition() | Reorders a range so elements that satisfy a condition come first. | C++98 | Try it |
stable_partition() | Reorders a range into two groups and keeps the order inside each group. | C++98 | Try it |
partition_copy() | Copies elements into two destinations depending on a condition. | C++11 | Try it |
partition_point() | Finds the border of a partitioned range. | C++11 | Try it |
is_partitioned() | Checks whether a range is partitioned by a condition. | C++11 | Try it |
Permutation
3 functionsTest or step through the different orderings of a range.
| Function | Description | Since | Tutorial |
|---|---|---|---|
is_permutation() | Checks whether one range is a rearrangement of another. | C++11 | Try it |
next_permutation() | Rearranges a range into the next permutation in dictionary order. | C++98 | Try it |
prev_permutation() | Rearranges a range into the previous permutation in dictionary order. | C++98 | Try it |
Frequently Asked Questions
Start with sort()
Open the sorting guide: syntax, three examples, and FAQs.
24 people found this page helpful
