C++ Algorithm Functions

Beginner
8 min read
Updated: Nov 2026
91 function guides
Function index

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().

Beginner path.

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:

C++
#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.

I want to…Use
Check that all, any, or none of the values matchall_of(), any_of(), none_of()
Find a value or the first matchfind(), find_if(), find_if_not(), search()
Count matchescount(), count_if()
Run code on every elementfor_each(), for_each_n(), transform()
Copy all or part of a rangecopy(), copy_if(), copy_n(), copy_backward()
Delete values from a vectorremove(), remove_if(), unique()
Sort datasort(), stable_sort(), partial_sort()
Find the median or the k-th valuenth_element()
Search sorted data quicklybinary_search(), lower_bound(), upper_bound(), equal_range()
Combine two sorted listsmerge(), set_union(), set_intersection(), set_difference()
Get the biggest or smallest valuemax_element(), min_element(), minmax_element(), max(), min(), clamp()
Reverse or rotate a rangereverse(), rotate(), shift_left()
Mix up data randomlyshuffle(), sample()
Split data by a conditionpartition(), stable_partition(), partition_copy()
Try every ordering of the datanext_permutation(), prev_permutation()
Use a vector like a priority queuemake_heap(), push_heap(), pop_heap()
Fill a range with valuesfill(), fill_n(), generate(), generate_n()
Compare two rangesequal(), mismatch(), lexicographical_compare()
Swap valuesswap(), swap_ranges(), iter_swap()
Replace valuesreplace(), replace_if(), replace_copy()

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 functions

Look at the data without changing it: test conditions, find values, and compare ranges.

FunctionDescriptionSinceTutorial
all_of()Checks whether every element in a range matches a condition.C++11Try it
any_of()Checks whether at least one element in a range matches a condition.C++11Try it
none_of()Checks that no element in a range matches a condition.C++11Try it
find()Finds the first element in a range that equals a value.C++98Try it
find_if()Finds the first element that satisfies a condition.C++98Try it
find_if_not()Finds the first element that does not satisfy a condition.C++11Try it
find_end()Finds the last occurrence of a sequence inside a range.C++98Try it
find_first_of()Finds the first element that matches any value from a second range.C++98Try it
adjacent_find()Finds the first two neighbouring elements that are equal.C++98Try it
search()Finds the first occurrence of a sequence inside a range.C++98Try it
search_n()Finds the first run of n equal elements in a row.C++98Try it
mismatch()Finds the first position where two ranges differ.C++98Try it
equal()Checks whether two ranges hold the same elements in the same order.C++98Try it
lexicographical_compare()Checks whether one range comes before another in dictionary order.C++98Try it
lexicographical_compare_three_way()Compares two ranges in dictionary order and returns less, equal, or greater.C++20Try it

Count & visit

4 functions

Count matching elements or run a function on each element.

FunctionDescriptionSinceTutorial
count()Counts how many elements equal a given value.C++98Try it
count_if()Counts how many elements satisfy a condition.C++98Try it
for_each()Calls a function on every element in a range.C++98Try it
for_each_n()Calls a function on the first n elements of a range.C++17Try it

Copy, move & swap (modifying)

9 functions

Copy or move elements to another place, or exchange values.

FunctionDescriptionSinceTutorial
copy()Copies the elements of one range into another place.C++98Try it
copy_backward()Copies a range into a destination, working from the last element to the first.C++98Try it
copy_if()Copies only the elements that satisfy a condition.C++11Try it
copy_n()Copies a fixed number of elements to another place.C++11Try it
move()Moves the elements of a range into another place instead of copying them.C++11Try it
move_backward()Moves a range into a destination, starting from the last element.C++11Try it
swap()Swaps the values of two objects.C++98Try it
swap_ranges()Swaps the elements of two ranges one by one.C++98Try it
iter_swap()Swaps the values that two iterators point to.C++98Try it

Fill, generate, transform & replace (modifying)

9 functions

Write new values into a range, or change values that already exist.

FunctionDescriptionSinceTutorial
fill()Assigns the same value to every element in a range.C++98Try it
fill_n()Assigns a value to the first n elements of a range.C++98Try it
generate()Fills a range with values made by calling a function.C++98Try it
generate_n()Fills n elements with values made by calling a function.C++98Try it
transform()Applies a function to each element and stores the results.C++98Try it
replace()Replaces every element equal to one value with another value.C++98Try it
replace_if()Replaces every element that satisfies a condition.C++98Try it
replace_copy()Copies a range while replacing one value with another.C++98Try it
replace_copy_if()Copies a range while replacing the elements that satisfy a condition.C++98Try it

Remove & unique (modifying)

6 functions

Drop unwanted elements and collapse repeated neighbours. Pair them with erase().

FunctionDescriptionSinceTutorial
remove()Moves all elements not equal to a value to the front and returns the new end.C++98Try it
remove_if()Moves elements that do not satisfy a condition to the front and returns the new end.C++98Try it
remove_copy()Copies a range while leaving out elements equal to a value.C++98Try it
remove_copy_if()Copies a range while leaving out elements that satisfy a condition.C++98Try it
unique()Removes consecutive duplicate elements and returns the new end.C++98Try it
unique_copy()Copies a range while collapsing consecutive duplicate elements.C++98Try it

Reverse, rotate, shift & shuffle (modifying)

9 functions

Change the order of elements without sorting them.

FunctionDescriptionSinceTutorial
reverse()Reverses the order of the elements in a range.C++98Try it
reverse_copy()Copies a range into a destination in reverse order.C++98Try it
rotate()Rotates the elements of a range so a chosen element becomes the first.C++98Try it
rotate_copy()Copies a range into a destination as if it was rotated.C++98Try it
shift_left()Moves the elements of a range toward the front by n places.C++20Try it
shift_right()Moves the elements of a range toward the back by n places.C++20Try it
shuffle()Randomly reorders the elements of a range using a random engine.C++11Try it
random_shuffle()Randomly reorders a range using rand() (removed in C++17).C++98Try it
sample()Picks n random elements from a range.C++17Try it

Sorting

7 functions

Sort a whole range, only the first part, or check if it is already sorted.

FunctionDescriptionSinceTutorial
sort()Sorts the elements of a range into ascending order.C++98Try it
stable_sort()Sorts a range and keeps the order of equal elements.C++98Try it
partial_sort()Sorts only the smallest elements to the front of a range.C++98Try it
partial_sort_copy()Copies the smallest elements of a range into a sorted destination.C++98Try it
nth_element()Puts the element that would be at position n in a sorted range into place.C++98Try it
is_sorted()Checks whether a range is sorted.C++11Try it
is_sorted_until()Finds the first element that breaks the sorted order.C++11Try it

Merge & set operations (sorted ranges)

7 functions

Combine two sorted ranges, or treat them like mathematical sets.

FunctionDescriptionSinceTutorial
merge()Merges two sorted ranges into one sorted range.C++98Try it
inplace_merge()Merges two consecutive sorted parts of one range in place.C++98Try it
includes()Checks whether a sorted range contains all elements of another sorted range.C++98Try it
set_union()Builds the union of two sorted ranges.C++98Try it
set_intersection()Builds the intersection of two sorted ranges.C++98Try it
set_difference()Builds the difference of two sorted ranges: elements in the first but not the second.C++98Try it
set_symmetric_difference()Builds the symmetric difference of two sorted ranges: elements in exactly one of them.C++98Try it

Heap

6 functions

Keep the largest (or smallest) element at the front of a vector.

FunctionDescriptionSinceTutorial
make_heap()Rearranges a range into a heap.C++98Try it
push_heap()Adds the last element of a range into an existing heap.C++98Try it
pop_heap()Moves the top element of a heap to the end of the range.C++98Try it
sort_heap()Turns a heap into a sorted range.C++98Try it
is_heap()Checks whether a range is a heap.C++11Try it
is_heap_until()Finds the first element that breaks the heap rule.C++11Try it

Min & max

7 functions

Find the smallest or largest value, or limit a value to a range.

FunctionDescriptionSinceTutorial
min()Returns the smaller of two values, or the smallest in a list.C++98Try it
max()Returns the larger of two values, or the largest in a list.C++98Try it
minmax()Returns the smaller and the larger of two values (or of a list) at once.C++11Try it
min_element()Finds the smallest element in a range.C++98Try it
max_element()Finds the largest element in a range.C++98Try it
minmax_element()Finds the smallest and the largest elements of a range in one pass.C++11Try it
clamp()Limits a value so it stays between a low and a high bound.C++17Try it

Partition

5 functions

Split a range into two groups by a condition.

FunctionDescriptionSinceTutorial
partition()Reorders a range so elements that satisfy a condition come first.C++98Try it
stable_partition()Reorders a range into two groups and keeps the order inside each group.C++98Try it
partition_copy()Copies elements into two destinations depending on a condition.C++11Try it
partition_point()Finds the border of a partitioned range.C++11Try it
is_partitioned()Checks whether a range is partitioned by a condition.C++11Try it

Permutation

3 functions

Test or step through the different orderings of a range.

FunctionDescriptionSinceTutorial
is_permutation()Checks whether one range is a rearrangement of another.C++11Try it
next_permutation()Rearranges a range into the next permutation in dictionary order.C++98Try it
prev_permutation()Rearranges a range into the previous permutation in dictionary order.C++98Try it

Frequently Asked Questions

They are ready-made functions in the <algorithm> header, such as std::sort(), std::find(), and std::count(). They work on ranges of elements, so one function can handle a vector, an array, or a string.
The Algorithms page teaches the basic ideas: iterators, ranges, predicates, and a starter example. This Functions index lists every function guide so you can open sort(), find(), merge(), and the rest.
Use #include <algorithm> for all functions on this page. A few helpers, such as std::accumulate() and std::iota(), live in <numeric> and are covered in the Numeric guides.
A range is written as [first, last). The last iterator points just after the final element. This makes empty ranges easy to describe, and lets algorithms like std::find() return v.end() when nothing matches.
No. It moves the elements you want to keep to the front and returns the new end. Call erase() with that iterator to shrink the container. This is called the erase-remove idiom.
Read the C++ Algorithms page first. Then open sort(), find(), count(), count_if(), and reverse() from the tables below.

Start with sort()

Open the sorting guide: syntax, three examples, and FAQs.

sort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

24 people found this page helpful