Skip to main content

Algorithms

The C++ algorithm library is a collection of functions that are designed to make it easier to write correct, efficient, expressive and reusable code.

It provides a set of common, highly optimized algorithms that can be used to perform a variety of tasks, such as searching for an item in a list, sorting a collection of data, or manipulating the contents of a container.

One of the main benefits of using the algorithm library is that it allows you to write code that is easier to understand and less error prone. All the algorithms have almost universally recognized names which helps understanding what certain parts of code using them do without having to do a lot of digging. Because the algorithms from the standard library have been thoroughly tested and optimized, you can rely on them to work correctly and efficiently. This can save you time and effort when developing software and it can help to reduce the risk of introducing bugs into your code.

Another benefit of the algorithm library is that it promotes code reuse. By using the algorithms provided by the library, you can avoid having to reimplement common operations, which can save time and make it easier to maintain your code.

Overall, the C++ algorithm library is an important tool for C++ programmers that makes it easier to write reliable, efficient, and reusable code.

Usage

To use the algorithms you have to include the <algorithm> header, like so:

#include <algorithm>

Then you can use them in code:

main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers = { 5, 12, 2137, 1, -5, 22, 96 };
std::ranges::sort(numbers);

for(int number : numbers) {
std::cout << number << ' ';
}
}
Result (console)
-5 1 5 12 22 96 2137
Rangified vs ordinary version

As you can see in the example above, there are two versions of algorithms. The rangified one and the ordinary one. The rangified version came to C++ with the C++20 standard. If you have a new enough compiler, you should be able to use it.

The basic difference between the two is that the ordinary version only accepts iterators, while the rangified version is more convenient and allows one to pass the whole range and enables the use of projections.

The rangified version is also less error-prone, as you can't pass invalid iterators by mistake (it also handles a few advanced things better than the ordinary version).

All that being said, using the rangified version is more beneficial and convenient, so if you have a possibility of choosing so, prefer the rangified version.

Ranges and iterators

Iterators

Iteration is the action of repeating a process, in case of containers, iteration means traversing the container in a particular order while obtaining each of it's elements.

Iterators are special objects with pointer semantics (having semantics of something basically means acting like something, pointer semantics mean that the iterator objects act like pointers in most ways, they can be dereferenced, incremented, etc.. Pointers are also valid iterators), that are used to iterate over containers (like strings, arrays, maps, sets, etc.).

They are obtained via the begin() and end() functions, and their variants:

  • cbegin()/cend() - return a const iterator to a container (you cannot change the contents via it)
  • rbegin()/rend() - return a reverse iterator to a container (iteration in reverse order)
  • crbegin()/crend() - return a const reverse iterator to a container (iteration in reverse order and you cannot change the contents via it)

Not every container has all the variations, for example std::unordered_set doesn't have the r and cr versions.

Different iterators have different types and they work a little bit differently, but the most basic things you can do with an iterator is:

  • increment it (it++ and ++it), which traverses onto the next object in a container
  • dereference it (*it), which returns the value that the iterator points to

Here are a few examples of using iterators with std::vector:

main.cpp
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers = { 5, 12, 2137, 1, -5, 22, 96 };

// We obtain the iterator here,
// we use the `c` version because we don't intend to change the contents of the vector,
// we only want to print it
std::vector<int>::const_iterator begin = numbers.cbegin();
std::vector<int>::const_iterator end = numbers.cend();

// here we first check if begin is not equal to end, if it is, then we've traversed the entire container
// if it's not, we print the number
// then we increment the iterator
for(; begin != end; ++begin) {
// *begin means "access the element the iterator points to"
std::cout << *begin << ' ';
}
}
Result (console)
5 12 2137 1 -5 22 96

Now, notice how we had to manually specify the type (std::vector<int>::iterator, std::vector<int>::reverse_iterator, etc.) every time. It is not very convenient. For that purpose you can use the auto placeholder:

auto begin = numbers.begin();
auto begin = numbers.rbegin();
auto begin = numbers.crbegin();

auto deduces the type for us and it has no performance penalty. The code is easier to read that way, so if you can, prefer it. Read more about it here: auto.

Ranges

Now what exactly is a range? The exact definition and inner workings in C++ are a little bit complicated, but a range can be defined as anything that we can iterate over. An array, vector, string, map, etc..

All rangified algorithms accept a range. The same way iterators can be different types and have slightly different behaviours, some ranges also differ from others and cannot be used with some algorithms.

List of algorithms

Searching

pubranges::find (since C++20)Finds the first element satisfying specific criteria.
pubfindFinds the first element satisfying specific criteria.
pubranges::find_if (since C++20)Finds the first element satisfying specific predicate.
pubfind_ifFinds the first element satisfying specific predicate.
pubranges::find_if_not (since C++20)Finds the first element **not** satisfying specific predicate.
pubfind_if_not (since C++11)Finds the first element **not** satisfying specific predicate.
pubranges::find_first_of (since C++20)Searches for any of the elements from one range in another.
pubfind_first_ofSearches for any of the elements from one range in another.
pubranges::find_end (since C++20)Finds the last sequence of elements in a certain range.
pubfind_endFinds the last sequence of elements in a certain range.
pubranges::find_last (since C++20)Finds the last element equal to some value.
pubranges::find_last_if (since C++20)Finds the last element satisfying specific criteria.
pubranges::find_last_if_not (since C++20)Finds the last element **not** satisfying specific criteria.
pubranges::adjacent_find (since C++20)Finds the first two adjacent items that satisfy a condition.
pubadjacent_findFinds the first two adjacent items that satisfy a condition.
pubranges::search (since C++20)Search for a range of elements.
pubsearchSearch for a range of elements.
pubranges::search_n (since C++20)Searches a range for a consecutive sequence of an element.
pubsearch_nSearches a range for a consecutive sequence of an element.

Determining conditions

pubranges::all_of (since C++20)Checks if all elements in a range hold a condition.
puball_of (since C++11)Checks if all elements in a range hold a condition.
pubranges::any_of (since C++20)Checks if any elements in a range hold a condition.
pubany_of (since C++11)Checks if any elements in a range hold a condition.
pubranges::none_of (since C++20)Checks if no elements in a range hold a condition.
pubnone_of (since C++11)Checks if no elements in a range hold a condition.
pubranges::count (since C++20)Returns the number of certain elements.
pubcountReturns the number of certain elements.
pubranges::count_if (since C++20)Returns the number of elements satisfying specific criteria.
pubcount_ifReturns the number of elements satisfying specific criteria.
pubranges::mismatch (since C++20)Finds the first position where two ranges differ.
pubmismatchFinds the first position where two ranges differ.
pubranges::starts_with (since C++20)Checks whether a range starts with another range.
pubranges::ends_with (since C++20)Checks whether a range ends with another range.

Other non-modifying

pubranges::for_each (since C++20)Applies a function to a range of elements.
pubfor_eachApplies a function to a range of elements.
pubranges::for_each_n (since C++20)Applies a function to a range of elements consisting of an iterator and a size.
pubfor_each_n (since C++17)Applies a function to a range of elements consisting of an iterator and a size.

Random operations

pubrandom_shuffle (removed in C++17)Deprecated in C++14 and removed in C++17, use std::shuffle instead.
Randomly re-orders elements in a range.
pubranges::shuffle (since C++20)Randomly re-orders elements in a range.
pubshuffle (since C++11)Randomly re-orders elements in a range.
Preffered method over std::random_shuffle.
pubranges::sample (since C++20)Selects n random elements from a sequence.
pubsample (since C++17)Selects n random elements from a sequence.

Modifying

pubswapSwaps the values of two objects.
pubranges::copy (since C++20)Copies a range of elements to a new location.
pubcopyCopies a range of elements to a new location.
pubranges::copy_if (since C++20)Copies elements from a range that satisfy a condition to a new location.
pubcopy_if (since C++11)Copies elements from a range that satisfy a condition to a new location.
pubranges::copy_n (since C++20)Copies a number elements from a range to a new location.
pubcopy_n (since C++11)Copies a number elements from a range to a new location.
pubranges::copy_backward (since C++20)Copies a range of elements in backwards order.
pubcopy_backwardCopies a range of elements in backwards order.
pubranges::move (since C++20)Moves a range of elements to a new location.
pubmoveMoves a range of elements to a new location.
pubranges::move_backward (since C++20)Moves a range of elements to a new location in backwards order.
pubmove_backwardMoves a range of elements to a new location in backwards order.
pubranges::transform (since C++20)Applies a function to a range of elements.
pubtransformApplies a function to a range of elements.
pubranges::reverse (since C++20)Reverses the order of elements.
pubreverseReverses the order of elements.
pubranges::reverse_copy (since C++20)Creates a reversed copy of a range.
pubreverse_copyCreates a reversed copy of a range.
pubranges::fill (since C++20)Copy-assigns the given value to every element in a range.
pubfillCopy-assigns the given value to every element in a range.
pubranges::fill_n (since C++20)Copy-assigns a value to a number of elements.
pubfill_nCopy-assigns a value to a number of elements.
pubranges::generate (since C++20)Assigns the results of successive function calls to every element in a range.
pubgenerateAssigns the results of successive function calls to every element in a range.
pubranges::remove (since C++20)Removes elements equal to value.
pubremoveRemoves elements equal to value.
pubranges::remove_if (since C++20)Removes elements satisfying specific criteria.
pubremove_ifRemoves elements satisfying specific criteria.
pubranges::remove_copy (since C++20)Copies a range of elements omitting those that are equal to value.
pubremove_copyCopies a range of elements omitting those that are equal to value.
pubranges::remove_copy_if (since C++20)Copies a range of elements omitting those that satisfy a predicate.
pubremove_copy_ifCopies a range of elements omitting those that satisfy a predicate.
pubranges::replace (since C++20)Replaces old values with new values.
pubreplaceReplaces old values with new values.
pubranges::replace_if (since C++20)Replaces values satisfying a predicate with new values.
pubreplace_ifReplaces values satisfying a predicate with new values.
pubranges::replace_copy (since C++20)Copies a range, replacing elements equal to old value with new value.
pubreplace_copyCopies a range, replacing elements equal to old value with new value.
pubranges::replace_copy_if (since C++20)Copies a range, replacing elements that satisfy a predicate with new value.
pubreplace_copy_ifCopies a range, replacing elements that satisfy a predicate with new value.
pubranges::swap_ranges (since C++20)Swaps two ranges.
pubswap_rangesSwaps two ranges.
pubiter_swapSwaps the values of the elements the given iterators are pointing to.
pubranges::rotate (since C++20)Rotates elements in a range.
pubrotateRotates elements in a range.
pubranges::rotate_copy (since C++20)Copies and rotates a range of elements.
pubrotate_copyCopies and rotates a range of elements.
pubranges::shift_left (since C++23)Shifts elements in a range left.
pubshift_left (since C++20)Shifts elements in a range left.
pubranges::shift_right (since C++23)Shifts elements in a range right.
pubshift_right (since C++20)Shifts elements in a range right.
pubranges::unique (since C++20)Removes consecutive duplicates of an element in a range.
pubuniqueRemoves consecutive duplicates of an element in a range.
pubranges::unique_copy (since C++20)Creates a copy of range with no consecutive duplicates.
pubunique_copyCreates a copy of range with no consecutive duplicates.

Partitioning

pubranges::is_partitioned (since C++20)Determines if the range is partitioned by the given predicate.
pubis_partitioned (since C++11)Determines if the range is partitioned by the given predicate.
pubranges::partition (since C++20)Divides a range of elements into two groups based on a predicate.
pubpartition (since C++11)Divides a range of elements into two groups based on a predicate.
pubranges::partition_copy (since C++20)Copies a range dividing the elements into two groups.
pubpartition_copy (since C++11)Copies a range dividing the elements into two groups.
pubranges::stable_partition (since C++20)Divides elements into two groups while preserving their relative order.
pubstable_partitionDivides elements into two groups while preserving their relative order.
pubranges::partition_point (since C++20)Locates the partition point of a range.
pubpartition_point (since C++11)Locates the partition point of a range.

Sorting

pubranges::sort (since C++20)Sorts a range into (by default) ascending order.
pubsortSorts a range into (by default) ascending order.
pubranges::stable_sort (since C++20)Sorts a range into (by default) ascending order while preserving the order between equal elements.
pubstable_sortSorts a range into (by default) ascending order while preserving the order between equal elements.
pubranges::partial_sort (since C++20)Sorts the first N elements of a range.
pubpartial_sortSorts the first N elements of a range.
pubranges::partial_sort_copy (since C++20)Copies and partially sorts a range of elements.
pubpartial_sort_copyCopies and partially sorts a range of elements.
pubranges::stable_sort (since C++20)Partially sorts the given range making sure that it is partitioned by the given element.
pubstable_sortPartially sorts the given range making sure that it is partitioned by the given element.
pubranges::is_sorted (since C++20)Checks whether a range is sorted.
pubis_sortedChecks whether a range is sorted.
pubranges::is_sorted_until (since C++20)Finds the largest sorted subrange.
pubis_sorted_untilFinds the largest sorted subrange.

Binary search (on sorted ranges)

pubranges::binary_search (since C++20)Searches for an element in a partially-ordered range.
pubbinary_searchSearches for an element in a partially-ordered range.
pubranges::lower_bound (since C++20)Returns an iterator to the first element not less than the given value.
publower_boundReturns an iterator to the first element not less than the given value.
pubranges::upper_bound (since C++20)Returns an iterator to the first element greater than the given value.
pubupper_boundReturns an iterator to the first element greater than the given value.
pubranges::equal_range (since C++20)Returns range of elements matching a specific key.
pubequal_rangeReturns range of elements matching a specific key.

Set (on sorted ranges)

pubranges::includes (since C++20)Returns true if a sequence is a subsequence of another.
pubincludesReturns true if a sequence is a subsequence of another.
pubranges::set_difference (since C++20)Computes the difference between two sets.
pubset_differenceComputes the difference between two sets.
pubranges::set_intersection (since C++20)Computes the intersection between two sets.
pubset_intersectionComputes the intersection between two sets.
pubranges::set_symmetric_difference (since C++20)Computes the symmetric difference between two sets.
pubset_symmetric_differenceComputes the symmetric difference between two sets.
pubranges::set_union (since C++20)Computes the union between two sets.
pubset_unionComputes the union between two sets.

Other operations on sorted ranges

pubranges::merge (since C++20)Merges two sorted ranges.
pubmergeMerges two sorted ranges.
pubranges::inplace_merge (since C++20)Merges two sorted ranges in-place.
pubinplace_mergeMerges two sorted ranges in-place.

Heap

pubranges::is_heap (since C++20)Checks if the given range is a max heap.
pubis_heapChecks if the given range is a max heap.
pubranges::is_heap_until (since C++20)Checks if the given range is a max heap.
pubis_heap_untilChecks if the given range is a max heap.
pubranges::make_heap (since C++20)Creates a max heap out of a range of elements.
pubmake_heapCreates a max heap out of a range of elements.
pubranges::push_heap (since C++20)Adds an element to a max heap.
pubpush_heapAdds an element to a max heap.
pubranges::pop_heap (since C++20)Adds an element to a max heap.
pubpop_heapAdds an element to a max heap.
pubranges::sort_heap (since C++20)Turns a max heap into a range of elements sorted in ascending order.
pubsort_heapTurns a max heap into a range of elements sorted in ascending order.

Min/max

pubranges::min (since C++20)Returns the smallest of the given values.
pubminReturns the smallest of the given values.
pubranges::max (since C++20)Returns the greatest of the given values.
pubmaxReturns the greatest of the given values.
pubranges::max_element (since C++20)Returns the largest element in a range.
pubmax_elementReturns the largest element in a range.
pubranges::min_element (since C++20)Returns the smallest element in a range.
pubmin_elementReturns the smallest element in a range.
pubranges::minmax (since C++20)Returns the smallest and largest from the given values.
pubminmaxReturns the smallest and largest from the given values.
pubranges::minmax_element (since C++20)Returns the smallest and the largest elements in a range.
pubminmax_elementReturns the smallest and the largest elements in a range.
pubranges::clamp (since C++20)Clamps a value between a pair of boundary values.
pubclampClamps a value between a pair of boundary values.

Comparison

pubranges::equal (since C++20)Determines if two sets of elements are the same.
pubequalDetermines if two sets of elements are the same.
pubranges::lexicographical_compare (since C++20)Returns true if one range is lexicographically less than another.
publexicographical_compareReturns true if one range is lexicographically less than another.
publexicographical_compare_three_wayCompares two ranges using three-way comparison.

Permutation

pub ranges::is_permutation  (since C++20)Determines if a sequence is a permutation of another sequence.
pub is_permutation  (since C++11)Determines if a sequence is a permutation of another sequence.
pub ranges::next_permutation  (since C++20)Generates the next greater lexicographic permutation of a range of elements.
pub next_permutation  (since C++11)Generates the next greater lexicographic permutation of a range of elements.
pub ranges::prev_permutation  (since C++20)Generates the prev smaller lexicographic permutation of a range of elements.
pub prev_permutation  (since C++11)Generates the prev smaller lexicographic permutation of a range of elements.

Numeric

pub ranges::iota  (since C++23)Fills a range with successive increments of the starting value.
pub iota  (since C++11)Fills a range with successive increments of the starting value.
pubaccumulateSums up or folds/reduces a range of elements.
pubinner_productComputes the inner product of two ranges of elements.
pubadjacent_differenceComputes the differences between adjacent elements in a range.
pubpartial_sumComputes the partial sum of a range of elements.
pubreduce (since C++17)Similar to std::accumulate, except out of order.
pubexclusive_scan (since C++17)Similar to std::partial_sum, excludes the ith input element from the ith sum.
pubinclusive_scan (since C++17)Similar to std::partial_sum, includes the ith input element from the ith sum.
pubtransform_reduce (since C++17)Applies an invocable, then reduces out of order.
pubtransform_exclusive_scan (since C++17)Applies an invocable, then calculates exclusive scan.
pubtransform_inclusive_scan (since C++17)Applies an invocable, then calculates inclusive scan.

Operations on uninitialized memory

pubuninitialized_copyCopies a range of objects to an uninitialized area of memory.
pubuninitialized_copyCopies a range of objects to an uninitialized area of memory.
pubuninitialized_copy_nCopies an amount of elements from a range to an uninitialized area of memory.
pubuninitialized_copy_nCopies an amount of elements from a range to an uninitialized area of memory.
pubuninitialized_fillFills an uninitialized range.
pubuninitialized_fillFills an uninitialized range.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_move (since C++17)Moves a number of objects to an uninitialized area of memory.
pubuninitialized_moveMoves a number of objects to an uninitialized area of memory.
pubuninitialized_move_n (since C++17)Moves a number of objects to an uninitialized area of memory.
pubuninitialized_move_nMoves a number of objects to an uninitialized area of memory.
pubuninitialized_default_construct (since C++17)Constructs objects by default-initialization in an uninitialized area of memory, defined by a range.
pubuninitialized_default_constructMoves a number of objects to an uninitialized area of memory.
pubuninitialized_default_construct_n (since C++17)Constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count.
pubuninitialized_default_construct_nConstructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count.
pubuninitialized_value_construct (since C++17)Constructs objects by value-initialization in an uninitialized area of memory, defined by a range.
pubuninitialized_value_constructConstructs objects by value-initialization in an uninitialized area of memory, defined by a range.
pubuninitialized_value_construct_n (since C++17)Constructs objects by value-initialization in an uninitialized area of memory, defined by a start and size.
pubuninitialized_value_construct_nConstructs objects by value-initialization in an uninitialized area of memory, defined by a start and size.
pubdestroy (since C++17)Destroys a range of objects.
pubdestroyDestroys a range of objects.
pubdestroy_n (since C++17)Destroys a number of objects.
pubdestroy_nDestroys a number of objects.
pubdestroy_at (since C++17)Destroys an object at a given address.
pubdestroy_atDestroys an object at a given address.
pubconstruct_at (since C++17)Creates an object at a given address.
pubconstruct_atCreates an object at a given address.

C algorithms

pubqsortSorts a range of elements with unspecified type.
pubbsearchSearches an array for an element of unspecified type.

Algorithms

The C++ algorithm library is a collection of functions that are designed to make it easier to write correct, efficient, expressive and reusable code.

It provides a set of common, highly optimized algorithms that can be used to perform a variety of tasks, such as searching for an item in a list, sorting a collection of data, or manipulating the contents of a container.

One of the main benefits of using the algorithm library is that it allows you to write code that is easier to understand and less error prone. All the algorithms have almost universally recognized names which helps understanding what certain parts of code using them do without having to do a lot of digging. Because the algorithms from the standard library have been thoroughly tested and optimized, you can rely on them to work correctly and efficiently. This can save you time and effort when developing software and it can help to reduce the risk of introducing bugs into your code.

Another benefit of the algorithm library is that it promotes code reuse. By using the algorithms provided by the library, you can avoid having to reimplement common operations, which can save time and make it easier to maintain your code.

Overall, the C++ algorithm library is an important tool for C++ programmers that makes it easier to write reliable, efficient, and reusable code.

Usage

To use the algorithms you have to include the <algorithm> header, like so:

#include <algorithm>

Then you can use them in code:

main.cpp
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers = { 5, 12, 2137, 1, -5, 22, 96 };
std::ranges::sort(numbers);

for(int number : numbers) {
std::cout << number << ' ';
}
}
Result (console)
-5 1 5 12 22 96 2137
Rangified vs ordinary version

As you can see in the example above, there are two versions of algorithms. The rangified one and the ordinary one. The rangified version came to C++ with the C++20 standard. If you have a new enough compiler, you should be able to use it.

The basic difference between the two is that the ordinary version only accepts iterators, while the rangified version is more convenient and allows one to pass the whole range and enables the use of projections.

The rangified version is also less error-prone, as you can't pass invalid iterators by mistake (it also handles a few advanced things better than the ordinary version).

All that being said, using the rangified version is more beneficial and convenient, so if you have a possibility of choosing so, prefer the rangified version.

Ranges and iterators

Iterators

Iteration is the action of repeating a process, in case of containers, iteration means traversing the container in a particular order while obtaining each of it's elements.

Iterators are special objects with pointer semantics (having semantics of something basically means acting like something, pointer semantics mean that the iterator objects act like pointers in most ways, they can be dereferenced, incremented, etc.. Pointers are also valid iterators), that are used to iterate over containers (like strings, arrays, maps, sets, etc.).

They are obtained via the begin() and end() functions, and their variants:

  • cbegin()/cend() - return a const iterator to a container (you cannot change the contents via it)
  • rbegin()/rend() - return a reverse iterator to a container (iteration in reverse order)
  • crbegin()/crend() - return a const reverse iterator to a container (iteration in reverse order and you cannot change the contents via it)

Not every container has all the variations, for example std::unordered_set doesn't have the r and cr versions.

Different iterators have different types and they work a little bit differently, but the most basic things you can do with an iterator is:

  • increment it (it++ and ++it), which traverses onto the next object in a container
  • dereference it (*it), which returns the value that the iterator points to

Here are a few examples of using iterators with std::vector:

main.cpp
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers = { 5, 12, 2137, 1, -5, 22, 96 };

// We obtain the iterator here,
// we use the `c` version because we don't intend to change the contents of the vector,
// we only want to print it
std::vector<int>::const_iterator begin = numbers.cbegin();
std::vector<int>::const_iterator end = numbers.cend();

// here we first check if begin is not equal to end, if it is, then we've traversed the entire container
// if it's not, we print the number
// then we increment the iterator
for(; begin != end; ++begin) {
// *begin means "access the element the iterator points to"
std::cout << *begin << ' ';
}
}
Result (console)
5 12 2137 1 -5 22 96

Now, notice how we had to manually specify the type (std::vector<int>::iterator, std::vector<int>::reverse_iterator, etc.) every time. It is not very convenient. For that purpose you can use the auto placeholder:

auto begin = numbers.begin();
auto begin = numbers.rbegin();
auto begin = numbers.crbegin();

auto deduces the type for us and it has no performance penalty. The code is easier to read that way, so if you can, prefer it. Read more about it here: auto.

Ranges

Now what exactly is a range? The exact definition and inner workings in C++ are a little bit complicated, but a range can be defined as anything that we can iterate over. An array, vector, string, map, etc..

All rangified algorithms accept a range. The same way iterators can be different types and have slightly different behaviours, some ranges also differ from others and cannot be used with some algorithms.

List of algorithms

Searching

pubranges::find (since C++20)Finds the first element satisfying specific criteria.
pubfindFinds the first element satisfying specific criteria.
pubranges::find_if (since C++20)Finds the first element satisfying specific predicate.
pubfind_ifFinds the first element satisfying specific predicate.
pubranges::find_if_not (since C++20)Finds the first element **not** satisfying specific predicate.
pubfind_if_not (since C++11)Finds the first element **not** satisfying specific predicate.
pubranges::find_first_of (since C++20)Searches for any of the elements from one range in another.
pubfind_first_ofSearches for any of the elements from one range in another.
pubranges::find_end (since C++20)Finds the last sequence of elements in a certain range.
pubfind_endFinds the last sequence of elements in a certain range.
pubranges::find_last (since C++20)Finds the last element equal to some value.
pubranges::find_last_if (since C++20)Finds the last element satisfying specific criteria.
pubranges::find_last_if_not (since C++20)Finds the last element **not** satisfying specific criteria.
pubranges::adjacent_find (since C++20)Finds the first two adjacent items that satisfy a condition.
pubadjacent_findFinds the first two adjacent items that satisfy a condition.
pubranges::search (since C++20)Search for a range of elements.
pubsearchSearch for a range of elements.
pubranges::search_n (since C++20)Searches a range for a consecutive sequence of an element.
pubsearch_nSearches a range for a consecutive sequence of an element.

Determining conditions

pubranges::all_of (since C++20)Checks if all elements in a range hold a condition.
puball_of (since C++11)Checks if all elements in a range hold a condition.
pubranges::any_of (since C++20)Checks if any elements in a range hold a condition.
pubany_of (since C++11)Checks if any elements in a range hold a condition.
pubranges::none_of (since C++20)Checks if no elements in a range hold a condition.
pubnone_of (since C++11)Checks if no elements in a range hold a condition.
pubranges::count (since C++20)Returns the number of certain elements.
pubcountReturns the number of certain elements.
pubranges::count_if (since C++20)Returns the number of elements satisfying specific criteria.
pubcount_ifReturns the number of elements satisfying specific criteria.
pubranges::mismatch (since C++20)Finds the first position where two ranges differ.
pubmismatchFinds the first position where two ranges differ.
pubranges::starts_with (since C++20)Checks whether a range starts with another range.
pubranges::ends_with (since C++20)Checks whether a range ends with another range.

Other non-modifying

pubranges::for_each (since C++20)Applies a function to a range of elements.
pubfor_eachApplies a function to a range of elements.
pubranges::for_each_n (since C++20)Applies a function to a range of elements consisting of an iterator and a size.
pubfor_each_n (since C++17)Applies a function to a range of elements consisting of an iterator and a size.

Random operations

pubrandom_shuffle (removed in C++17)Deprecated in C++14 and removed in C++17, use std::shuffle instead.
Randomly re-orders elements in a range.
pubranges::shuffle (since C++20)Randomly re-orders elements in a range.
pubshuffle (since C++11)Randomly re-orders elements in a range.
Preffered method over std::random_shuffle.
pubranges::sample (since C++20)Selects n random elements from a sequence.
pubsample (since C++17)Selects n random elements from a sequence.

Modifying

pubswapSwaps the values of two objects.
pubranges::copy (since C++20)Copies a range of elements to a new location.
pubcopyCopies a range of elements to a new location.
pubranges::copy_if (since C++20)Copies elements from a range that satisfy a condition to a new location.
pubcopy_if (since C++11)Copies elements from a range that satisfy a condition to a new location.
pubranges::copy_n (since C++20)Copies a number elements from a range to a new location.
pubcopy_n (since C++11)Copies a number elements from a range to a new location.
pubranges::copy_backward (since C++20)Copies a range of elements in backwards order.
pubcopy_backwardCopies a range of elements in backwards order.
pubranges::move (since C++20)Moves a range of elements to a new location.
pubmoveMoves a range of elements to a new location.
pubranges::move_backward (since C++20)Moves a range of elements to a new location in backwards order.
pubmove_backwardMoves a range of elements to a new location in backwards order.
pubranges::transform (since C++20)Applies a function to a range of elements.
pubtransformApplies a function to a range of elements.
pubranges::reverse (since C++20)Reverses the order of elements.
pubreverseReverses the order of elements.
pubranges::reverse_copy (since C++20)Creates a reversed copy of a range.
pubreverse_copyCreates a reversed copy of a range.
pubranges::fill (since C++20)Copy-assigns the given value to every element in a range.
pubfillCopy-assigns the given value to every element in a range.
pubranges::fill_n (since C++20)Copy-assigns a value to a number of elements.
pubfill_nCopy-assigns a value to a number of elements.
pubranges::generate (since C++20)Assigns the results of successive function calls to every element in a range.
pubgenerateAssigns the results of successive function calls to every element in a range.
pubranges::remove (since C++20)Removes elements equal to value.
pubremoveRemoves elements equal to value.
pubranges::remove_if (since C++20)Removes elements satisfying specific criteria.
pubremove_ifRemoves elements satisfying specific criteria.
pubranges::remove_copy (since C++20)Copies a range of elements omitting those that are equal to value.
pubremove_copyCopies a range of elements omitting those that are equal to value.
pubranges::remove_copy_if (since C++20)Copies a range of elements omitting those that satisfy a predicate.
pubremove_copy_ifCopies a range of elements omitting those that satisfy a predicate.
pubranges::replace (since C++20)Replaces old values with new values.
pubreplaceReplaces old values with new values.
pubranges::replace_if (since C++20)Replaces values satisfying a predicate with new values.
pubreplace_ifReplaces values satisfying a predicate with new values.
pubranges::replace_copy (since C++20)Copies a range, replacing elements equal to old value with new value.
pubreplace_copyCopies a range, replacing elements equal to old value with new value.
pubranges::replace_copy_if (since C++20)Copies a range, replacing elements that satisfy a predicate with new value.
pubreplace_copy_ifCopies a range, replacing elements that satisfy a predicate with new value.
pubranges::swap_ranges (since C++20)Swaps two ranges.
pubswap_rangesSwaps two ranges.
pubiter_swapSwaps the values of the elements the given iterators are pointing to.
pubranges::rotate (since C++20)Rotates elements in a range.
pubrotateRotates elements in a range.
pubranges::rotate_copy (since C++20)Copies and rotates a range of elements.
pubrotate_copyCopies and rotates a range of elements.
pubranges::shift_left (since C++23)Shifts elements in a range left.
pubshift_left (since C++20)Shifts elements in a range left.
pubranges::shift_right (since C++23)Shifts elements in a range right.
pubshift_right (since C++20)Shifts elements in a range right.
pubranges::unique (since C++20)Removes consecutive duplicates of an element in a range.
pubuniqueRemoves consecutive duplicates of an element in a range.
pubranges::unique_copy (since C++20)Creates a copy of range with no consecutive duplicates.
pubunique_copyCreates a copy of range with no consecutive duplicates.

Partitioning

pubranges::is_partitioned (since C++20)Determines if the range is partitioned by the given predicate.
pubis_partitioned (since C++11)Determines if the range is partitioned by the given predicate.
pubranges::partition (since C++20)Divides a range of elements into two groups based on a predicate.
pubpartition (since C++11)Divides a range of elements into two groups based on a predicate.
pubranges::partition_copy (since C++20)Copies a range dividing the elements into two groups.
pubpartition_copy (since C++11)Copies a range dividing the elements into two groups.
pubranges::stable_partition (since C++20)Divides elements into two groups while preserving their relative order.
pubstable_partitionDivides elements into two groups while preserving their relative order.
pubranges::partition_point (since C++20)Locates the partition point of a range.
pubpartition_point (since C++11)Locates the partition point of a range.

Sorting

pubranges::sort (since C++20)Sorts a range into (by default) ascending order.
pubsortSorts a range into (by default) ascending order.
pubranges::stable_sort (since C++20)Sorts a range into (by default) ascending order while preserving the order between equal elements.
pubstable_sortSorts a range into (by default) ascending order while preserving the order between equal elements.
pubranges::partial_sort (since C++20)Sorts the first N elements of a range.
pubpartial_sortSorts the first N elements of a range.
pubranges::partial_sort_copy (since C++20)Copies and partially sorts a range of elements.
pubpartial_sort_copyCopies and partially sorts a range of elements.
pubranges::stable_sort (since C++20)Partially sorts the given range making sure that it is partitioned by the given element.
pubstable_sortPartially sorts the given range making sure that it is partitioned by the given element.
pubranges::is_sorted (since C++20)Checks whether a range is sorted.
pubis_sortedChecks whether a range is sorted.
pubranges::is_sorted_until (since C++20)Finds the largest sorted subrange.
pubis_sorted_untilFinds the largest sorted subrange.

Binary search (on sorted ranges)

pubranges::binary_search (since C++20)Searches for an element in a partially-ordered range.
pubbinary_searchSearches for an element in a partially-ordered range.
pubranges::lower_bound (since C++20)Returns an iterator to the first element not less than the given value.
publower_boundReturns an iterator to the first element not less than the given value.
pubranges::upper_bound (since C++20)Returns an iterator to the first element greater than the given value.
pubupper_boundReturns an iterator to the first element greater than the given value.
pubranges::equal_range (since C++20)Returns range of elements matching a specific key.
pubequal_rangeReturns range of elements matching a specific key.

Set (on sorted ranges)

pubranges::includes (since C++20)Returns true if a sequence is a subsequence of another.
pubincludesReturns true if a sequence is a subsequence of another.
pubranges::set_difference (since C++20)Computes the difference between two sets.
pubset_differenceComputes the difference between two sets.
pubranges::set_intersection (since C++20)Computes the intersection between two sets.
pubset_intersectionComputes the intersection between two sets.
pubranges::set_symmetric_difference (since C++20)Computes the symmetric difference between two sets.
pubset_symmetric_differenceComputes the symmetric difference between two sets.
pubranges::set_union (since C++20)Computes the union between two sets.
pubset_unionComputes the union between two sets.

Other operations on sorted ranges

pubranges::merge (since C++20)Merges two sorted ranges.
pubmergeMerges two sorted ranges.
pubranges::inplace_merge (since C++20)Merges two sorted ranges in-place.
pubinplace_mergeMerges two sorted ranges in-place.

Heap

pubranges::is_heap (since C++20)Checks if the given range is a max heap.
pubis_heapChecks if the given range is a max heap.
pubranges::is_heap_until (since C++20)Checks if the given range is a max heap.
pubis_heap_untilChecks if the given range is a max heap.
pubranges::make_heap (since C++20)Creates a max heap out of a range of elements.
pubmake_heapCreates a max heap out of a range of elements.
pubranges::push_heap (since C++20)Adds an element to a max heap.
pubpush_heapAdds an element to a max heap.
pubranges::pop_heap (since C++20)Adds an element to a max heap.
pubpop_heapAdds an element to a max heap.
pubranges::sort_heap (since C++20)Turns a max heap into a range of elements sorted in ascending order.
pubsort_heapTurns a max heap into a range of elements sorted in ascending order.

Min/max

pubranges::min (since C++20)Returns the smallest of the given values.
pubminReturns the smallest of the given values.
pubranges::max (since C++20)Returns the greatest of the given values.
pubmaxReturns the greatest of the given values.
pubranges::max_element (since C++20)Returns the largest element in a range.
pubmax_elementReturns the largest element in a range.
pubranges::min_element (since C++20)Returns the smallest element in a range.
pubmin_elementReturns the smallest element in a range.
pubranges::minmax (since C++20)Returns the smallest and largest from the given values.
pubminmaxReturns the smallest and largest from the given values.
pubranges::minmax_element (since C++20)Returns the smallest and the largest elements in a range.
pubminmax_elementReturns the smallest and the largest elements in a range.
pubranges::clamp (since C++20)Clamps a value between a pair of boundary values.
pubclampClamps a value between a pair of boundary values.

Comparison

pubranges::equal (since C++20)Determines if two sets of elements are the same.
pubequalDetermines if two sets of elements are the same.
pubranges::lexicographical_compare (since C++20)Returns true if one range is lexicographically less than another.
publexicographical_compareReturns true if one range is lexicographically less than another.
publexicographical_compare_three_wayCompares two ranges using three-way comparison.

Permutation

pub ranges::is_permutation  (since C++20)Determines if a sequence is a permutation of another sequence.
pub is_permutation  (since C++11)Determines if a sequence is a permutation of another sequence.
pub ranges::next_permutation  (since C++20)Generates the next greater lexicographic permutation of a range of elements.
pub next_permutation  (since C++11)Generates the next greater lexicographic permutation of a range of elements.
pub ranges::prev_permutation  (since C++20)Generates the prev smaller lexicographic permutation of a range of elements.
pub prev_permutation  (since C++11)Generates the prev smaller lexicographic permutation of a range of elements.

Numeric

pub ranges::iota  (since C++23)Fills a range with successive increments of the starting value.
pub iota  (since C++11)Fills a range with successive increments of the starting value.
pubaccumulateSums up or folds/reduces a range of elements.
pubinner_productComputes the inner product of two ranges of elements.
pubadjacent_differenceComputes the differences between adjacent elements in a range.
pubpartial_sumComputes the partial sum of a range of elements.
pubreduce (since C++17)Similar to std::accumulate, except out of order.
pubexclusive_scan (since C++17)Similar to std::partial_sum, excludes the ith input element from the ith sum.
pubinclusive_scan (since C++17)Similar to std::partial_sum, includes the ith input element from the ith sum.
pubtransform_reduce (since C++17)Applies an invocable, then reduces out of order.
pubtransform_exclusive_scan (since C++17)Applies an invocable, then calculates exclusive scan.
pubtransform_inclusive_scan (since C++17)Applies an invocable, then calculates inclusive scan.

Operations on uninitialized memory

pubuninitialized_copyCopies a range of objects to an uninitialized area of memory.
pubuninitialized_copyCopies a range of objects to an uninitialized area of memory.
pubuninitialized_copy_nCopies an amount of elements from a range to an uninitialized area of memory.
pubuninitialized_copy_nCopies an amount of elements from a range to an uninitialized area of memory.
pubuninitialized_fillFills an uninitialized range.
pubuninitialized_fillFills an uninitialized range.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_fill_nFills an uninitialized range defined by start and count.
pubuninitialized_move (since C++17)Moves a number of objects to an uninitialized area of memory.
pubuninitialized_moveMoves a number of objects to an uninitialized area of memory.
pubuninitialized_move_n (since C++17)Moves a number of objects to an uninitialized area of memory.
pubuninitialized_move_nMoves a number of objects to an uninitialized area of memory.
pubuninitialized_default_construct (since C++17)Constructs objects by default-initialization in an uninitialized area of memory, defined by a range.
pubuninitialized_default_constructMoves a number of objects to an uninitialized area of memory.
pubuninitialized_default_construct_n (since C++17)Constructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count.
pubuninitialized_default_construct_nConstructs objects by default-initialization in an uninitialized area of memory, defined by a start and a count.
pubuninitialized_value_construct (since C++17)Constructs objects by value-initialization in an uninitialized area of memory, defined by a range.
pubuninitialized_value_constructConstructs objects by value-initialization in an uninitialized area of memory, defined by a range.
pubuninitialized_value_construct_n (since C++17)Constructs objects by value-initialization in an uninitialized area of memory, defined by a start and size.
pubuninitialized_value_construct_nConstructs objects by value-initialization in an uninitialized area of memory, defined by a start and size.
pubdestroy (since C++17)Destroys a range of objects.
pubdestroyDestroys a range of objects.
pubdestroy_n (since C++17)Destroys a number of objects.
pubdestroy_nDestroys a number of objects.
pubdestroy_at (since C++17)Destroys an object at a given address.
pubdestroy_atDestroys an object at a given address.
pubconstruct_at (since C++17)Creates an object at a given address.
pubconstruct_atCreates an object at a given address.

C algorithms

pubqsortSorts a range of elements with unspecified type.
pubbsearchSearches an array for an element of unspecified type.