The algorithm and numeric headers provide functions that operate on a range of elements. The range is given by a start and end iterator, where the end is not included:
#include <algorithm>
#include <numeric>Some examples use these functions:
bool is_even(int x) {
return x % 2 == 0;
}
void print(int x) {
std::cout#include <iostream> << x << "\n";
}In some examples, a ? means an element with an unspecified value.
Most algorithms also have a version in the std::ranges#include <ranges> namespace that takes a whole container instead of a pair of iterators. Some of them return a std::ranges::subrange#include <ranges> or a struct instead of a single iterator.
Non-modifying
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
std::vector#include <vector><int> w = {1, 4};
These algorithms read a range without changing it:
std::for_each#include <algorithm>(v.begin(), v.end(), print); // Prints 3, 1, 4, 1, 4, 4.
std::count#include <algorithm>(v.begin(), v.end(), 1); // 2
std::count_if#include <algorithm>(v.begin(), v.end(), is_even); // 3
std::find#include <algorithm>(v.begin(), v.end(), 4); // Points to v[2].
std::find_if#include <algorithm>(v.begin(), v.end(), is_even); // Points to v[2].
// Finds the first element that is equal to any element of w.
std::find_first_of#include <algorithm>(v.begin(), v.end(), w.begin(), w.end()); // Points to v[1].
// Finds the first occurrence of the sequence 1, 4.
std::search#include <algorithm>(v.begin(), v.end(), w.begin(), w.end()); // Points to v[1].
// Finds the last occurrence of the sequence 1, 4.
std::find_end#include <algorithm>(v.begin(), v.end(), w.begin(), w.end()); // Points to v[3].
// Finds the first run of two 4's.
std::search_n#include <algorithm>(v.begin(), v.end(), 2, 4); // Points to v[4].
// Finds the first two neighbours that are equal.
std::adjacent_find#include <algorithm>(v.begin(), v.end()); // Points to v[4].
// Finds the first position where v and w differ, as a pair of iterators.
std::mismatch#include <algorithm>(v.begin(), v.begin() + 2, w.begin()); // Pair of v[0] and w[0].
// Checks whether the first two elements of v are equal to the elements of w.
std::equal#include <algorithm>(v.begin(), v.begin() + 2, w.begin()); // falsestd::ranges::for_each#include <algorithm>(v, print); // Prints 3, 1, 4, 1, 4, 4.
std::ranges::count#include <algorithm>(v, 1); // 2
std::ranges::count_if#include <algorithm>(v, is_even); // 3
std::ranges::find#include <algorithm>(v, 4); // Points to v[2].
std::ranges::find_if#include <algorithm>(v, is_even); // Points to v[2].
// Finds the first element that is equal to any element of w.
std::ranges::find_first_of#include <algorithm>(v, w); // Points to v[1].
// Finds the first occurrence of the sequence 1, 4.
std::ranges::search#include <algorithm>(v, w); // Subrange of v[1] and v[2].
// Finds the last occurrence of the sequence 1, 4.
std::ranges::find_end#include <algorithm>(v, w); // Subrange of v[3] and v[4].
// Finds the first run of two 4's.
std::ranges::search_n#include <algorithm>(v, 2, 4); // Subrange of v[4] and v[5].
// Finds the first two neighbours that are equal.
std::ranges::adjacent_find#include <algorithm>(v); // Points to v[4].
// Finds the first position where v and w differ, stopping at the end of the shorter range.
std::ranges::mismatch#include <algorithm>(v, w); // Pair of v[0] and w[0].
// Checks whether the first two elements of v are equal to the elements of w.
std::ranges::equal#include <algorithm>(v | std::views::take#include <ranges>(2), w); // falsestd::all_of#include <algorithm>(v.begin(), v.end(), [](int x) { return x > 0; }); // true
std::any_of#include <algorithm>(v.begin(), v.end(), is_even); // true
std::none_of#include <algorithm>(v.begin(), v.end(), is_even); // false
// Finds the first element that doesn't match the predicate.
std::find_if_not#include <algorithm>(v.begin(), v.end(), is_even); // Points to v[0].std::ranges::all_of#include <algorithm>(v, [](int x) { return x > 0; }); // true
std::ranges::any_of#include <algorithm>(v, is_even); // true
std::ranges::none_of#include <algorithm>(v, is_even); // false
// Finds the first element that doesn't match the predicate.
std::ranges::find_if_not#include <algorithm>(v, is_even); // Points to v[0].std::for_each_n#include <algorithm>(v.begin(), 2, print); // Prints 3, 1.Modifying
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
std::vector#include <vector><int> w = {1, 4};
std::vector#include <vector><int> out(6);
These algorithms change the elements of a range, or write them to another range:
std::copy#include <algorithm>(v.begin(), v.end(), out.begin()); // Writes 3, 1, 4, 1, 4, 4.
// Copies so that the last element copied lands at the end of out.
std::copy_backward#include <algorithm>(v.begin(), v.begin() + 2, out.end()); // out is {0, 0, 0, 0, 3, 1}.
std::fill#include <algorithm>(v.begin(), v.end(), 0); // v is {0, 0, 0, 0, 0, 0}.
std::fill_n#include <algorithm>(v.begin(), 2, 0); // v is {0, 0, 4, 1, 4, 4}.
// Applies a function to each element and writes the results.
std::transform#include <algorithm>(v.begin(), v.end(), out.begin(), std::negate#include <functional><int>()); // Writes -3, -1, -4, -1, -4, -4.
// Assigns each element the result of calling a function.
std::generate#include <algorithm>(v.begin(), v.end(), std::rand#include <cstdlib>); // Fills v with random numbers.
std::generate_n#include <algorithm>(v.begin(), 2, std::rand#include <cstdlib>); // Sets v[0] and v[1] to random numbers.
// Moves the elements that aren't 1 to the front.
// Returns an iterator to the new end, which is v[4].
std::remove#include <algorithm>(v.begin(), v.end(), 1); // v is {3, 4, 4, 4, ?, ?}.
// Moves the elements that aren't even to the front.
// Returns an iterator to the new end, which is v[3].
std::remove_if#include <algorithm>(v.begin(), v.end(), is_even); // v is {3, 1, 1, ?, ?, ?}.
// Copies the elements that aren't 1.
std::remove_copy#include <algorithm>(v.begin(), v.end(), out.begin(), 1); // Writes 3, 4, 4, 4.
std::remove_copy_if#include <algorithm>(v.begin(), v.end(), out.begin(), is_even); // Writes 3, 1, 1.
std::replace#include <algorithm>(v.begin(), v.end(), 1, 9); // v is {3, 9, 4, 9, 4, 4}.
std::replace_if#include <algorithm>(v.begin(), v.end(), is_even, 0); // v is {3, 1, 0, 1, 0, 0}.
std::replace_copy#include <algorithm>(v.begin(), v.end(), out.begin(), 1, 9); // Writes 3, 9, 4, 9, 4, 4.
std::replace_copy_if#include <algorithm>(v.begin(), v.end(), out.begin(), is_even, 0); // Writes 3, 1, 0, 1, 0, 0.
// Swaps the first two elements of v with the elements of w.
std::swap_ranges#include <algorithm>(v.begin(), v.begin() + 2, w.begin()); // v is {1, 4, 4, 1, 4, 4} and w is {3, 1}.
// Swaps the two elements the iterators point to.
std::iter_swap#include <algorithm>(v.begin(), v.begin() + 1); // v is {1, 3, 4, 1, 4, 4}.
std::reverse#include <algorithm>(v.begin(), v.end()); // v is {4, 4, 1, 4, 1, 3}.
std::reverse_copy#include <algorithm>(v.begin(), v.end(), out.begin()); // Writes 4, 4, 1, 4, 1, 3.
// Rotates the elements left so that v[2] becomes the first element.
std::rotate#include <algorithm>(v.begin(), v.begin() + 2, v.end()); // v is {4, 1, 4, 4, 3, 1}.
std::rotate_copy#include <algorithm>(v.begin(), v.begin() + 2, v.end(), out.begin()); // Writes 4, 1, 4, 4, 3, 1.
// Removes adjacent duplicates, so both 1's stay but the last two 4's become one.
// Returns an iterator to the new end, which is v[5].
std::unique#include <algorithm>(v.begin(), v.end()); // v is {3, 1, 4, 1, 4, ?}.
// Copies the elements without adjacent duplicates.
std::unique_copy#include <algorithm>(v.begin(), v.end(), out.begin()); // Writes 3, 1, 4, 1, 4.std::ranges::copy#include <algorithm>(v, out.begin()); // Writes 3, 1, 4, 1, 4, 4.
// Copies so that the last element copied lands at the end of out.
std::ranges::copy_backward#include <algorithm>(v | std::views::take#include <ranges>(2), out.end()); // out is {0, 0, 0, 0, 3, 1}.
std::ranges::fill#include <algorithm>(v, 0); // v is {0, 0, 0, 0, 0, 0}.
std::ranges::fill_n#include <algorithm>(v.begin(), 2, 0); // v is {0, 0, 4, 1, 4, 4}.
// Applies a function to each element and writes the results.
std::ranges::transform#include <algorithm>(v, out.begin(), std::negate#include <functional>()); // Writes -3, -1, -4, -1, -4, -4.
// Assigns each element the result of calling a function.
std::ranges::generate#include <algorithm>(v, std::rand#include <cstdlib>); // Fills v with random numbers.
std::ranges::generate_n#include <algorithm>(v.begin(), 2, std::rand#include <cstdlib>); // Sets v[0] and v[1] to random numbers.
// Moves the elements that aren't 1 to the front.
// Returns the leftover elements as a subrange, which starts at v[4].
std::ranges::remove#include <algorithm>(v, 1); // v is {3, 4, 4, 4, ?, ?}.
// Moves the elements that aren't even to the front.
// Returns the leftover elements as a subrange, which starts at v[3].
std::ranges::remove_if#include <algorithm>(v, is_even); // v is {3, 1, 1, ?, ?, ?}.
// Copies the elements that aren't 1.
std::ranges::remove_copy#include <algorithm>(v, out.begin(), 1); // Writes 3, 4, 4, 4.
std::ranges::remove_copy_if#include <algorithm>(v, out.begin(), is_even); // Writes 3, 1, 1.
std::ranges::replace#include <algorithm>(v, 1, 9); // v is {3, 9, 4, 9, 4, 4}.
std::ranges::replace_if#include <algorithm>(v, is_even, 0); // v is {3, 1, 0, 1, 0, 0}.
std::ranges::replace_copy#include <algorithm>(v, out.begin(), 1, 9); // Writes 3, 9, 4, 9, 4, 4.
std::ranges::replace_copy_if#include <algorithm>(v, out.begin(), is_even, 0); // Writes 3, 1, 0, 1, 0, 0.
// Swaps elements of v and w, stopping at the end of the shorter range.
std::ranges::swap_ranges#include <algorithm>(v, w); // v is {1, 4, 4, 1, 4, 4} and w is {3, 1}.
// Swaps the two elements the iterators point to.
std::ranges::iter_swap#include <algorithm>(v.begin(), v.begin() + 1); // v is {1, 3, 4, 1, 4, 4}.
std::ranges::reverse#include <algorithm>(v); // v is {4, 4, 1, 4, 1, 3}.
std::ranges::reverse_copy#include <algorithm>(v, out.begin()); // Writes 4, 4, 1, 4, 1, 3.
// Rotates the elements left so that v[2] becomes the first element.
std::ranges::rotate#include <algorithm>(v, v.begin() + 2); // v is {4, 1, 4, 4, 3, 1}.
std::ranges::rotate_copy#include <algorithm>(v, v.begin() + 2, out.begin()); // Writes 4, 1, 4, 4, 3, 1.
// Removes adjacent duplicates, so both 1's stay but the last two 4's become one.
// Returns the leftover elements as a subrange, which starts at v[5].
std::ranges::unique#include <algorithm>(v); // v is {3, 1, 4, 1, 4, ?}.
// Copies the elements without adjacent duplicates.
std::ranges::unique_copy#include <algorithm>(v, out.begin()); // Writes 3, 1, 4, 1, 4.std::copy_if#include <algorithm>(v.begin(), v.end(), out.begin(), is_even); // Writes 4, 4, 4.
std::copy_n#include <algorithm>(v.begin(), 2, out.begin()); // Writes 3, 1.
// Moves each element instead of copying it.
std::move#include <algorithm>(v.begin(), v.end(), out.begin()); // Writes 3, 1, 4, 1, 4, 4.
// Moves so that the last element moved lands at the end of out.
std::move_backward#include <algorithm>(v.begin(), v.begin() + 2, out.end()); // out is {0, 0, 0, 0, 3, 1}.
std::shuffle#include <algorithm>(v.begin(), v.end(), std::mt19937#include <random>()); // Puts v in a random order.std::ranges::copy_if#include <algorithm>(v, out.begin(), is_even); // Writes 4, 4, 4.
std::ranges::copy_n#include <algorithm>(v.begin(), 2, out.begin()); // Writes 3, 1.
// Moves each element instead of copying it.
std::ranges::move#include <algorithm>(v, out.begin()); // Writes 3, 1, 4, 1, 4, 4.
// Moves so that the last element moved lands at the end of out.
std::ranges::move_backward#include <algorithm>(v | std::views::take#include <ranges>(2), out.end()); // out is {0, 0, 0, 0, 3, 1}.
std::ranges::shuffle#include <algorithm>(v, std::mt19937#include <random>()); // Puts v in a random order.// Picks random elements without repeats, keeping their original order.
std::sample#include <algorithm>(v.begin(), v.end(), out.begin(), 2, std::mt19937#include <random>()); // Writes 2 random elements of v.// Picks random elements without repeats, keeping their original order.
std::ranges::sample#include <algorithm>(v, out.begin(), 2, std::mt19937#include <random>()); // Writes 2 random elements of v.// Shifts the elements 2 places towards the front.
std::shift_left#include <algorithm>(v.begin(), v.end(), 2); // v is {4, 1, 4, 4, ?, ?}.
// Shifts the elements 2 places towards the back.
std::shift_right#include <algorithm>(v.begin(), v.end(), 2); // v is {?, ?, 3, 1, 4, 1}.// Shifts the elements 2 places towards the front.
std::ranges::shift_left#include <algorithm>(v, 2); // v is {4, 1, 4, 4, ?, ?}.
// Shifts the elements 2 places towards the back.
std::ranges::shift_right#include <algorithm>(v, 2); // v is {?, ?, 3, 1, 4, 1}.Partitioning
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
These algorithms move the elements that match a predicate in front of the ones that don't:
std::partition#include <algorithm>(v.begin(), v.end(), is_even); // Even elements first, in any order.
// Same as partition, but keeps the original order within each group.
std::stable_partition#include <algorithm>(v.begin(), v.end(), is_even); // v is {4, 4, 4, 3, 1, 1}.std::ranges::partition#include <algorithm>(v, is_even); // Even elements first, in any order.
// Same as partition, but keeps the original order within each group.
std::ranges::stable_partition#include <algorithm>(v, is_even); // v is {4, 4, 4, 3, 1, 1}.Let:
std::vector#include <vector><int> evens(6);
std::vector#include <vector><int> odds(6);// Checks whether every even element comes before every odd element.
std::is_partitioned#include <algorithm>(v.begin(), v.end(), is_even); // false
// Copies the even elements to one range and the odd elements to another.
std::partition_copy#include <algorithm>(v.begin(), v.end(), evens.begin(), odds.begin(), is_even); // Writes 4, 4, 4 to evens and 3, 1, 1 to odds.
// On a partitioned range, finds the first element that doesn't match.
std::stable_partition#include <algorithm>(v.begin(), v.end(), is_even); // v is {4, 4, 4, 3, 1, 1}.
std::partition_point#include <algorithm>(v.begin(), v.end(), is_even); // Points to v[3].// Checks whether every even element comes before every odd element.
std::ranges::is_partitioned#include <algorithm>(v, is_even); // false
// Copies the even elements to one range and the odd elements to another.
std::ranges::partition_copy#include <algorithm>(v, evens.begin(), odds.begin(), is_even); // Writes 4, 4, 4 to evens and 3, 1, 1 to odds.
// On a partitioned range, finds the first element that doesn't match.
std::ranges::stable_partition#include <algorithm>(v, is_even); // v is {4, 4, 4, 3, 1, 1}.
std::ranges::partition_point#include <algorithm>(v, is_even); // Points to v[3].Sorting
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
std::vector#include <vector><int> out(6);
std::sort#include <algorithm>(v.begin(), v.end()); // v is {1, 1, 3, 4, 4, 4}.
std::sort#include <algorithm>(v.begin(), v.end(), std::greater#include <functional><int>()); // v is {4, 4, 4, 3, 1, 1}.
// Sorts, keeping equal elements in their original order.
std::stable_sort#include <algorithm>(v.begin(), v.end()); // v is {1, 1, 3, 4, 4, 4}.
// Sorts only the 2 smallest elements into the front.
std::partial_sort#include <algorithm>(v.begin(), v.begin() + 2, v.end()); // v is {1, 1, ?, ?, ?, ?}.
// Copies the 2 smallest elements, sorted.
std::partial_sort_copy#include <algorithm>(v.begin(), v.end(), out.begin(), out.begin() + 2); // Writes 1, 1.
// Puts the element that belongs at v[2] after sorting there.
// Smaller elements go before it and larger elements after.
std::nth_element#include <algorithm>(v.begin(), v.begin() + 2, v.end()); // v[2] is 3.std::ranges::sort#include <algorithm>(v); // v is {1, 1, 3, 4, 4, 4}.
std::ranges::sort#include <algorithm>(v, std::ranges::greater#include <functional>()); // v is {4, 4, 4, 3, 1, 1}.
// Sorts, keeping equal elements in their original order.
std::ranges::stable_sort#include <algorithm>(v); // v is {1, 1, 3, 4, 4, 4}.
// Sorts only the 2 smallest elements into the front.
std::ranges::partial_sort#include <algorithm>(v, v.begin() + 2); // v is {1, 1, ?, ?, ?, ?}.
// Copies the 2 smallest elements, sorted.
std::ranges::partial_sort_copy#include <algorithm>(v, out | std::views::take#include <ranges>(2)); // Writes 1, 1.
// Puts the element that belongs at v[2] after sorting there.
// Smaller elements go before it and larger elements after.
std::ranges::nth_element#include <algorithm>(v, v.begin() + 2); // v[2] is 3.std::is_sorted#include <algorithm>(v.begin(), v.end()); // false
// Finds the first element that breaks the sorted order.
std::is_sorted_until#include <algorithm>(v.begin(), v.end()); // Points to v[1].std::ranges::is_sorted#include <algorithm>(v); // false
// Finds the first element that breaks the sorted order.
std::ranges::is_sorted_until#include <algorithm>(v); // Points to v[1].Binary Search
Let:
std::vector#include <vector><int> s = {1, 1, 3, 4, 4, 4};
These algorithms need a sorted range, and take O(log n) time on random access iterators:
// Finds the first element that isn't less than 3.
std::lower_bound#include <algorithm>(s.begin(), s.end(), 3); // Points to s[2].
// Finds the first element that is greater than 3.
std::upper_bound#include <algorithm>(s.begin(), s.end(), 3); // Points to s[3].
std::binary_search#include <algorithm>(s.begin(), s.end(), 4); // true
// Finds the range of elements equal to 1, as a pair of lower_bound and upper_bound.
std::equal_range#include <algorithm>(s.begin(), s.end(), 1); // Pair of s[0] and s[2].// Finds the first element that isn't less than 3.
std::ranges::lower_bound#include <algorithm>(s, 3); // Points to s[2].
// Finds the first element that is greater than 3.
std::ranges::upper_bound#include <algorithm>(s, 3); // Points to s[3].
std::ranges::binary_search#include <algorithm>(s, 4); // true
// Finds the range of elements equal to 1, as a subrange from lower_bound to upper_bound.
std::ranges::equal_range#include <algorithm>(s, 1); // Subrange of s[0] and s[1].Merging and Sets
Let:
std::vector#include <vector><int> a = {1, 2, 3, 4};
std::vector#include <vector><int> b = {3, 4, 5};
std::vector#include <vector><int> c = {2, 5, 1, 3};
std::vector#include <vector><int> out(7);
These algorithms need sorted ranges:
// Combines two sorted ranges into one sorted range.
std::merge#include <algorithm>(a.begin(), a.end(), b.begin(), b.end(), out.begin()); // Writes 1, 2, 3, 3, 4, 4, 5.
// Merges the two sorted halves of c, {2, 5} and {1, 3}, in place.
std::inplace_merge#include <algorithm>(c.begin(), c.begin() + 2, c.end()); // c is {1, 2, 3, 5}.
// Checks whether every element of b is in a.
std::includes#include <algorithm>(a.begin(), a.end(), b.begin(), b.end()); // false
// Writes the elements in a but not in b.
std::set_difference#include <algorithm>(a.begin(), a.end(), b.begin(), b.end(), out.begin()); // Writes 1, 2.
// Writes the elements in both a and b.
std::set_intersection#include <algorithm>(a.begin(), a.end(), b.begin(), b.end(), out.begin()); // Writes 3, 4.
// Writes the elements in only one of a and b.
std::set_symmetric_difference#include <algorithm>(a.begin(), a.end(), b.begin(), b.end(), out.begin()); // Writes 1, 2, 5.
// Writes the elements in either a or b.
std::set_union#include <algorithm>(a.begin(), a.end(), b.begin(), b.end(), out.begin()); // Writes 1, 2, 3, 4, 5.// Combines two sorted ranges into one sorted range.
std::ranges::merge#include <algorithm>(a, b, out.begin()); // Writes 1, 2, 3, 3, 4, 4, 5.
// Merges the two sorted halves of c, {2, 5} and {1, 3}, in place.
std::ranges::inplace_merge#include <algorithm>(c, c.begin() + 2); // c is {1, 2, 3, 5}.
// Checks whether every element of b is in a.
std::ranges::includes#include <algorithm>(a, b); // false
// Writes the elements in a but not in b.
std::ranges::set_difference#include <algorithm>(a, b, out.begin()); // Writes 1, 2.
// Writes the elements in both a and b.
std::ranges::set_intersection#include <algorithm>(a, b, out.begin()); // Writes 3, 4.
// Writes the elements in only one of a and b.
std::ranges::set_symmetric_difference#include <algorithm>(a, b, out.begin()); // Writes 1, 2, 5.
// Writes the elements in either a or b.
std::ranges::set_union#include <algorithm>(a, b, out.begin()); // Writes 1, 2, 3, 4, 5.Heap
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
std::vector#include <vector><int> h = {5, 3, 4, 1, 2};
These algorithms arrange a range as a max heap, where the largest element is always at the front:
// Rearranges v into a max heap.
std::make_heap#include <algorithm>(v.begin(), v.end()); // v[0] is 4, which is the largest element.
// Moves the last element into its place in the heap.
// Call it after push_back().
h.push_back(9);
std::push_heap#include <algorithm>(h.begin(), h.end()); // h is {9, 3, 5, 1, 2, 4}.
// Moves the largest element to the back and keeps the rest a heap.
// Call pop_back() after it to remove the element.
std::pop_heap#include <algorithm>(h.begin(), h.end()); // h is {4, 3, 2, 1, 5}.
// Sorts a heap in ascending order.
std::sort_heap#include <algorithm>(h.begin(), h.end()); // h is {1, 2, 3, 4, 5}.// Rearranges v into a max heap.
std::ranges::make_heap#include <algorithm>(v); // v[0] is 4, which is the largest element.
// Moves the last element into its place in the heap.
// Call it after push_back().
h.push_back(9);
std::ranges::push_heap#include <algorithm>(h); // h is {9, 3, 5, 1, 2, 4}.
// Moves the largest element to the back and keeps the rest a heap.
// Call pop_back() after it to remove the element.
std::ranges::pop_heap#include <algorithm>(h); // h is {4, 3, 2, 1, 5}.
// Sorts a heap in ascending order.
std::ranges::sort_heap#include <algorithm>(h); // h is {1, 2, 3, 4, 5}.std::is_heap#include <algorithm>(v.begin(), v.end()); // false
std::is_heap#include <algorithm>(h.begin(), h.end()); // true
// Finds the first element that breaks the heap order.
// v[2] is 4, which is larger than its parent v[0].
std::is_heap_until#include <algorithm>(v.begin(), v.end()); // Points to v[2].std::ranges::is_heap#include <algorithm>(v); // false
std::ranges::is_heap#include <algorithm>(h); // true
// Finds the first element that breaks the heap order.
// v[2] is 4, which is larger than its parent v[0].
std::ranges::is_heap_until#include <algorithm>(v); // Points to v[2].Minimum and Maximum
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
std::vector#include <vector><int> w = {1, 4};
std::min#include <algorithm>(3, 5); // 3
std::max#include <algorithm>(3, 5); // 5
std::min_element#include <algorithm>(v.begin(), v.end()); // Points to v[1].
// Finds the first largest element.
std::max_element#include <algorithm>(v.begin(), v.end()); // Points to v[2].
// Checks whether v comes before w in dictionary order.
// It doesn't, since 3 > 1.
std::lexicographical_compare#include <algorithm>(v.begin(), v.end(), w.begin(), w.end()); // falsestd::ranges::min#include <algorithm>(3, 5); // 3
std::ranges::max#include <algorithm>(3, 5); // 5
std::ranges::min_element#include <algorithm>(v); // Points to v[1].
// Finds the first largest element.
std::ranges::max_element#include <algorithm>(v); // Points to v[2].
// Checks whether v comes before w in dictionary order.
// It doesn't, since 3 > 1.
std::ranges::lexicographical_compare#include <algorithm>(v, w); // falsestd::min#include <algorithm>({3, 1, 4}); // 1
std::minmax#include <algorithm>(3, 5); // Pair of 3 and 5.
// Finds the first smallest and the last largest element.
std::minmax_element#include <algorithm>(v.begin(), v.end()); // Pair of v[1] and v[5].std::ranges::min#include <algorithm>({3, 1, 4}); // 1
std::ranges::minmax#include <algorithm>(3, 5); // Struct with min 3 and max 5.
// Finds the first smallest and the last largest element.
std::ranges::minmax_element#include <algorithm>(v); // Struct with min v[1] and max v[5].// Limits 7 to the range 0 to 5.
std::clamp#include <algorithm>(7, 0, 5); // 5// Limits 7 to the range 0 to 5.
std::ranges::clamp#include <algorithm>(7, 0, 5); // 5// Compares v and w in dictionary order, like <=>.
std::lexicographical_compare_three_way#include <algorithm>(v.begin(), v.end(), w.begin(), w.end()); // std::strong_ordering#include <compare>::greaterPermutations
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
// Rearranges v into the next permutation in sorted order.
// Returns false if v was already the last one.
std::next_permutation#include <algorithm>(v.begin(), v.end()); // v is {3, 1, 4, 4, 1, 4}.
// Rearranges v into the previous permutation in sorted order.
// Returns false if v was already the first one.
std::prev_permutation#include <algorithm>(v.begin(), v.end()); // v is {3, 1, 1, 4, 4, 4}.// Rearranges v into the next permutation in sorted order.
// The result's found member is false if v was already the last one.
std::ranges::next_permutation#include <algorithm>(v); // v is {3, 1, 4, 4, 1, 4}.
// Rearranges v into the previous permutation in sorted order.
// The result's found member is false if v was already the first one.
std::ranges::prev_permutation#include <algorithm>(v); // v is {3, 1, 1, 4, 4, 4}.Let:
std::vector#include <vector><int> s = {1, 1, 3, 4, 4, 4};// Checks whether v and s hold the same elements, in any order.
std::is_permutation#include <algorithm>(v.begin(), v.end(), s.begin()); // true// Checks whether v and s hold the same elements, in any order.
std::ranges::is_permutation#include <algorithm>(v, s); // trueNumeric
Let:
std::vector#include <vector><int> v = {3, 1, 4, 1, 4, 4};
std::vector#include <vector><int> out(6);
These algorithms are available from the numeric header:
// Adds up the elements, starting from 0.
std::accumulate#include <numeric>(v.begin(), v.end(), 0); // 17
// Multiplies the two ranges element by element, then adds up the products.
std::inner_product#include <numeric>(v.begin(), v.end(), v.begin(), 0); // 59
// Writes the running total.
std::partial_sum#include <numeric>(v.begin(), v.end(), out.begin()); // Writes 3, 4, 8, 9, 13, 17.
// Writes the difference between each element and the one before it.
std::adjacent_difference#include <numeric>(v.begin(), v.end(), out.begin()); // Writes 3, -2, 3, -3, 3, 0.// Fills v with increasing values, starting from 1.
std::iota#include <numeric>(v.begin(), v.end(), 1); // v is {1, 2, 3, 4, 5, 6}.// Fills v with increasing values, starting from 1.
std::ranges::iota#include <numeric>(v, 1); // v is {1, 2, 3, 4, 5, 6}.Let:
auto square = [](int x) { return x * x; };// Like accumulate, but may add in any order, so it can run in parallel.
std::reduce#include <numeric>(v.begin(), v.end()); // 17
// Like inner_product, but may add in any order.
std::transform_reduce#include <numeric>(v.begin(), v.end(), v.begin(), 0); // 59
// Like partial_sum, but may add in any order.
std::inclusive_scan#include <numeric>(v.begin(), v.end(), out.begin()); // Writes 3, 4, 8, 9, 13, 17.
// Writes the running total before each element, starting from 0.
std::exclusive_scan#include <numeric>(v.begin(), v.end(), out.begin(), 0); // Writes 0, 3, 4, 8, 9, 13.
// Squares each element, then writes the running total.
std::transform_inclusive_scan#include <numeric>(v.begin(), v.end(), out.begin(), std::plus#include <functional><>(), square); // Writes 9, 10, 26, 27, 43, 59.
// Squares each element, then writes the running total before each element.
std::transform_exclusive_scan#include <numeric>(v.begin(), v.end(), out.begin(), 0, std::plus#include <functional><>(), square); // Writes 0, 9, 10, 26, 27, 43.
// Greatest common divisor.
std::gcd#include <numeric>(12, 18); // 6
// Lowest common multiple.
std::lcm#include <numeric>(4, 6); // 12// Halfway between two numbers, without overflowing.
std::midpoint#include <numeric>(2, 8); // 5
Dan