Section hub

Constrained algorithms (since C++20)

C++20 provides constrained versions of most algorithms in the namespace std::ranges. In these algorithms, a range can be specified as either an iterator-sentinel pair or as a single range argument, and projections and pointer-to-member callables are supported. Additionally, the return types of most algorithms have been changed to return all potentially useful information computed during the execution of the algorithm.

# Notes

Feature-test macro Value Std Feature __cpp_lib_algorithm_default_value_type 202403L (C++26) List-initialization for algorithms __cpp_lib_ranges 201911L (C++20) Ranges library and constrained algorithms __cpp_lib_ranges_contains 202207L (C++23) std::ranges::contains __cpp_lib_ranges_find_last 202207L (C++23) std::ranges::find_last __cpp_lib_ranges_fold 202207L (C++23) std::ranges fold algorithms __cpp_lib_ranges_iota 202202L (C++23) std::ranges::iota __cpp_lib_ranges_starts_ends_with 202106L (C++23) std::ranges::starts_with, std::ranges::ends_with __cpp_lib_shift 201806L (C++20) std::shift_left, std::shift_right 202202L (C++23) std::ranges::shift_left, std::ranges::shift_right __cpp_lib_ranges_generate_random 202403L (C++26) std::ranges::generate_random

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
P3136R1C++20niebloids were allowed to be specified as special entitiesother than function objectsrequired to be specified as function objects

This hub groups algorithms by task first, then points back to the canonical reference page. Ranges forms and execution-policy overloads stay attached to the same conceptual item.

Core algorithms

Start here for the small set of algorithms that anchor most day-to-day work: ordering, searching, transformation, partitioning, and numeric accumulation.

sort

ranges policy C++17

Sorts the elements in the range [first,last) in non-descending order. The order of equal elements is not guaranteed to be …

Canonical reference

stable_sort

ranges policy C++17

Sorts the elements in the range [first,last) in non-descending order. The order of equivalent elements is guaranteed to be …

Canonical reference

find

ranges policy C++26

Returns an iterator to the first element in the range [first,last) that satisfies specific criteria (or last if there is no such …

Canonical reference

lower_bound

ranges C++26

Searches for the first element in the partitioned range [first,last) which is not ordered before value. # Declarations …

Canonical reference

transform

ranges policy C++17

std::transform applies the given function to the elements of the given input range(s), and stores the result in an output range …

Canonical reference

copy

ranges policy C++17

Copies the elements in the range, defined by [first,last), to another range beginning at d_first (copy destination range). # …

Canonical reference

remove

ranges policy C++26

Removes all elements satisfying specific criteria from the range [first,last) and returns a past-the-end iterator for the new end …

Canonical reference

partition

ranges policy C++17

Reorders the elements in the range [first,last) in such a way that all elements for which the predicate p returns true precede …

Canonical reference

merge

ranges policy C++17

Merges two sorted ranges [first1,last1) and [first2,last2) into one sorted range beginning at d_first. # Declarations template< …

Canonical reference

Computes the sum of the given value init and the elements in the range [first,last). # Declarations template< class InputIt, …

Canonical reference

Full index by task

Each panel groups one algorithm family and keeps the rows compact enough to scan like an index rather than a card wall.

Searching and lookup

24
  • adjacent_find
    ranges policy C++17

    Searches the range [first,last) for two consecutive equal elements. # Declarations template< class …

  • all_of
    ranges policy C++17

    Checks if unary predicate p returns false for at least one element in the range [first,last). # Declarations …

  • any_of
    ranges policy C++17

    Checks if unary predicate p returns false for at least one element in the range [first,last). # Declarations …

  • binary_search
    ranges C++26

    Checks if an element equivalent to value appears within the partitioned range [first,last). # Declarations …

  • count
    ranges policy C++26

    Returns the number of elements in the range [first,last) satisfying specific criteria. # Declarations …

  • count_if
    ranges policy C++26

    Returns the number of elements in the range [first,last) satisfying specific criteria. # Declarations …

  • equal_range
    ranges C++26

    Returns a range containing all elements equivalent to value in the partitioned range [first,last). # …

  • find
    ranges policy C++26

    Returns an iterator to the first element in the range [first,last) that satisfies specific criteria (or last …

  • find_end
    ranges policy C++17

    Searches for the last occurrence of the sequence [s_first,s_last) in the range [first,last). # Declarations …

  • find_first_of
    ranges policy C++17

    Searches the range [first,last) for any of the elements in the range [s_first,s_last). # Declarations …

  • find_if
    ranges policy C++26

    Returns an iterator to the first element in the range [first,last) that satisfies specific criteria (or last …

  • find_if_not
    ranges policy C++26

    Returns an iterator to the first element in the range [first,last) that satisfies specific criteria (or last …

  • lower_bound
    ranges C++26

    Searches for the first element in the partitioned range [first,last) which is not ordered before value. # …

  • none_of
    ranges policy C++17

    Checks if unary predicate p returns false for at least one element in the range [first,last). # Declarations …

  • contains
    ranges C++26

    Search-based algorithm that checks whether or not a given range contains a value with iterator-sentinel …

  • contains_subrange
    ranges C++26

    Search-based algorithm that checks whether or not a given range contains a value with iterator-sentinel …

  • ends_with
    ranges C++23

    Checks whether the second range matches the suffix of the first range. # Declarations Call signature …

  • find_last
    ranges C++26

    Returns the last element in the range [first,last) that satisfies specific criteria: # Declarations Call …

  • find_last_if
    ranges C++26

    Returns the last element in the range [first,last) that satisfies specific criteria: # Declarations Call …

  • find_last_if_not
    ranges C++26

    Returns the last element in the range [first,last) that satisfies specific criteria: # Declarations Call …

  • starts_with
    ranges C++23

    Checks whether the second range matches the prefix of the first range. # Declarations Call signature …

  • search
    ranges policy C++17

    1-4) Searches for the first occurrence of the sequence of elements [s_first,s_last) in the range [first,last). …

  • search_n
    ranges policy C++26

    Searches the range [first,last) for the first sequence of count identical elements, each equal to the given …

  • upper_bound
    ranges C++26

    Searches for the first element in the partitioned range [first,last) which is ordered after value. # …

Partitioning

5
  • is_partitioned
    ranges policy C++17

    Checks whether [first,last) is partitioned by the predicate p: all elements satisfy p appear before all …

  • partition
    ranges policy C++17

    Reorders the elements in the range [first,last) in such a way that all elements for which the predicate p …

  • partition_copy
    ranges policy C++17

    Copies the elements from the range [first,last) to two different ranges depending on the value returned by …

  • partition_point
    ranges C++20

    Examines the partitioned range [first,last) and locates the end of the first partition, that is, the first …

  • stable_partition
    ranges policy C++17

    Reorders the elements in the range [first,last) in such a way that all elements for which the predicate p …

Set operations and merge

7
  • includes
    ranges policy C++17

    Returns true if the sorted range [first2,last2) is a subsequence of the sorted range [first1,last1) (a …

  • inplace_merge
    ranges policy C++17

    Merges two consecutive sorted ranges [first,middle) and [middle,last) into one sorted range [first,last). # …

  • merge
    ranges policy C++17

    Merges two sorted ranges [first1,last1) and [first2,last2) into one sorted range beginning at d_first. # …

  • set_difference
    ranges policy C++17

    Copies the elements from the sorted range [first1,last1) which are not found in the sorted range …

  • set_intersection
    ranges policy C++17

    Constructs a sorted range beginning at d_first consisting of elements that are found in both sorted ranges …

  • set_symmetric_difference
    ranges policy C++17

    Computes symmetric difference of two sorted ranges: the elements that are found in either of the ranges, but …

  • set_union
    ranges policy C++17

    Constructs a sorted union beginning at d_first consisting of the set of elements present in one or both sorted …

Numeric and scans

17
  • Computes the sum of the given value init and the elements in the range [first,last). # Declarations …

  • Let T be the value type of decltype(first). # Declarations template< class InputIt, class OutputIt > …

  • exclusive_scan
    policy C++17

    Equivalent to exclusive_scan(first, last, d_first, init, std::plus<>(). # Declarations template< …

  • inclusive_scan
    policy C++17

    Equivalent to inclusive_scan(first, last, d_first, std::plus<>(). # Declarations template< class …

  • Computes inner product (i.e. sum of products) or performs ordered map/reduce operation on the range …

  • iota
    ranges C++23

    Fills the range [first,last) with sequentially increasing values, starting with value and repetitively …

  • If [first,last) is empty, does nothing. # Declarations template< class InputIt, class OutputIt > …

  • fold_left
    ranges C++26

    Left-folds the elements of given range, that is, returns the result of evaluation of the chain …

  • fold_left_first
    ranges C++23

    Left-folds the elements of given range, that is, returns the result of evaluation of the chain …

  • Left-folds the elements of given range, that is, returns the result of evaluation of the chain …

  • Left-folds the elements of given range, that is, returns the result of evaluation of the chain …

  • fold_right
    ranges C++26

    Right-folds the elements of given range, that is, returns the result of evaluation of the chain …

  • fold_right_last
    ranges C++23

    Right-folds the elements of given range, that is, returns the result of evaluation of the chain …

  • reduce
    policy C++17

    Equivalent to reduce(first, last, typename std::iterator_traits::value_type{}). # Declarations template< …

  • Computes the exclusive prefix sum using op. # Declarations template< class InputIt, class OutputIt, class …

  • Computes the inclusive prefix sum using op. # Declarations template< class InputIt, class OutputIt, class …

  • transform_reduce
    policy C++17

    Equivalent to transform_reduce(first1, last1, first2, init,std::plus<>(), std::multiplies<>()), …

Permutations

6
  • is_permutation
    ranges C++20

    Checks whether [first1,last1) is a permutation of a range starting from first2: # Declarations template< …

  • next_permutation
    ranges C++20

    Permutes the range [first,last) into the next permutation. Returns true if such a “next permutation” exists; …

  • prev_permutation
    ranges C++20

    Transforms the range [first,last) into the previous permutation. Returns true if such permutation exists, …

  • Reorders the elements in the given range [first,last) such that each possible permutation of those elements …

  • sample
    ranges C++17

    Selects n elements from the sequence [first,last) (without replacement) such that each possible sample has …

  • shuffle
    ranges C++20

    Reorders the elements in the given range [first,last) such that each possible permutation of those elements …

Sorting and ordering

7
  • is_sorted
    ranges policy C++17

    Checks if the elements in range [first,last) are sorted in non-descending order. # Declarations template< …

  • is_sorted_until
    ranges policy C++17

    Examines the range [first,last) and finds the largest range beginning at first in which the elements are …

  • nth_element
    ranges policy C++17

    nth_element rearranges elements in [first,last) such that after the rearrangement: # Declarations template< …

  • partial_sort
    ranges policy C++17

    Rearranges elements such that the range [first,middle) contains the sorted middle − first smallest elements in …

  • partial_sort_copy
    ranges policy C++17

    Sorts some of the elements in the range [first,last) in ascending order, storing the result in the range …

  • sort
    ranges policy C++17

    Sorts the elements in the range [first,last) in non-descending order. The order of equal elements is not …

  • stable_sort
    ranges policy C++17

    Sorts the elements in the range [first,last) in non-descending order. The order of equivalent elements is …

Modifying sequences

33
  • copy
    ranges policy C++17

    Copies the elements in the range, defined by [first,last), to another range beginning at d_first (copy …

  • copy_backward
    ranges C++20

    Copies the elements from the range [first,last) to another range ending at d_last. The elements are copied in …

  • copy_if
    ranges policy C++17

    Copies the elements in the range, defined by [first,last), to another range beginning at d_first (copy …

  • copy_n
    ranges policy C++17

    Copies exactly count values from the range beginning at first to the range beginning at result. Formally, for …

  • fill
    ranges policy C++26

    Assigns the given value to all elements in the range [first,last). # Declarations template< class …

  • fill_n
    ranges policy C++26

    Assigns the given value to the first count elements in the range beginning at first if count > 0. Does …

  • for_each
    ranges policy C++17

    Applies the given function object f to the result of dereferencing every iterator in the range [first,last). …

  • for_each_n
    ranges policy C++17

    Applies the given function object f to the result of dereferencing every iterator in the range [first,first + …

  • generate
    ranges policy C++17

    Assigns each element in range [first,last) a value generated by the given function object g. # Declarations …

  • generate_n
    ranges policy C++17

    Assigns values, generated by given function object g, to the first count elements in the range beginning at …

  • Swaps the values of the elements the given iterators are pointing to. # Declarations template< class …

  • move
    ranges policy C++17

    Moves the elements in the range [first,last), to another range beginning at d_first, starting from first and …

  • move_backward
    ranges C++20

    Moves the elements from the range [first,last), to another range ending at d_last. The elements are moved in …

  • generate_random
    ranges C++26

    Attempts to generate random numbers with the generate_random member function of the random number generator or …

  • remove
    ranges policy C++26

    Removes all elements satisfying specific criteria from the range [first,last) and returns a past-the-end …

  • remove_copy
    ranges policy C++26

    Copies elements from the range [first,last), to another range beginning at d_first, omitting the elements …

  • remove_copy_if
    ranges policy C++26

    Copies elements from the range [first,last), to another range beginning at d_first, omitting the elements …

  • remove_if
    ranges policy C++26

    Removes all elements satisfying specific criteria from the range [first,last) and returns a past-the-end …

  • replace
    ranges policy C++26

    Replaces all elements in the range [first,last) with new_value if they satisfy specific criteria. # …

  • replace_copy
    ranges policy C++17

    Copies the elements from the range [first,last) to another range beginning at d_first, while replacing all …

  • replace_copy_if
    ranges policy C++17

    Copies the elements from the range [first,last) to another range beginning at d_first, while replacing all …

  • replace_if
    ranges policy C++26

    Replaces all elements in the range [first,last) with new_value if they satisfy specific criteria. # …

  • reverse
    ranges policy C++17

    Reverses the order of the elements in the range [first,last). # Declarations template< class BidirIt > …

  • reverse_copy
    ranges policy C++17

    Given (\scriptsize N)N as std::distance(first, last). Copies the elements from the range [first,last) (source …

  • rotate
    ranges policy C++17

    Performs a left rotation on a range of elements. # Declarations template< class ForwardIt > ForwardIt …

  • rotate_copy
    ranges policy C++17

    Copies the elements from the range [first,last), to another range beginning at d_first in such a way, that …

  • shift_left
    ranges policy C++20

    Shifts the elements in the range [first,last) by n positions. # Declarations template< class ForwardIt > …

  • shift_right
    ranges policy C++20

    Shifts the elements in the range [first,last) by n positions. # Declarations template< class ForwardIt > …

  • Exchanges the given values. # Declarations template< class T > void swap( T& a, T& b ); …

  • swap_ranges
    ranges policy C++17

    Exchanges elements between range [first1,last1) and another range of std::distance(first1, last1) elements …

  • transform
    ranges policy C++17

    std::transform applies the given function to the elements of the given input range(s), and stores the result …

  • unique
    ranges policy C++17

    Removes all except the first element from every consecutive group of equivalent elements from the range …

  • unique_copy
    ranges policy C++17

    Copies the elements from the range [first,last), to another range beginning at d_first in such a way that …

Comparison and min/max

11
  • clamp
    ranges C++17

    If the value of v is within [lo,hi], returns v; otherwise returns the nearest boundary. # Declarations …

  • equal
    ranges policy C++17

    Checks whether [first1,last1) and a range starting from first2 are equal: # Declarations template< class …

  • lexicographical_compare
    ranges policy C++17

    Checks if the first range [first1,last1) is lexicographically less than the second range [first2,last2). # …

  • Lexicographically compares two ranges [first1,last1) and [first2,last2) using three-way comparison and …

  • max
    ranges C++20

    Returns the greater of the given values. # Declarations template< class T > const T& max( const …

  • max_element
    ranges policy C++17

    Finds the greatest element in the range [first,last). # Declarations template< class ForwardIt > …

  • min
    ranges C++20

    Returns the smaller of the given values. # Declarations template< class T > const T& min( const …

  • min_element
    ranges policy C++17

    Finds the smallest element in the range [first,last). # Declarations template< class ForwardIt > …

  • minmax
    ranges C++20

    Returns the lowest and the greatest of the given values. # Declarations template< class T > …

  • minmax_element
    ranges policy C++17

    Finds the smallest and greatest element in the range [first,last). # Declarations template< class ForwardIt …

  • mismatch
    ranges policy C++17

    Returns a pair of iterators to the first mismatching of elements from [first1,last1) and a range starting from …

Heap and priority structure

6
  • is_heap
    ranges policy C++17

    Checks whether [first,last) is a heap. # Declarations template< class RandomIt > bool is_heap( RandomIt …

  • is_heap_until
    ranges policy C++17

    Examines the range [first,last) and finds the largest range beginning at first which is a heap. # Declarations …

  • make_heap
    ranges C++20

    Constructs a heap in the range [first,last). # Declarations template< class RandomIt > void make_heap( …

  • pop_heap
    ranges C++20

    Swaps the value in the position first and the value in the position last - 1 and makes the subrange …

  • push_heap
    ranges C++20

    Inserts the element at the position last - 1 into the heap [first,last - 1). The heap after the insertion will …

  • sort_heap
    ranges C++20

    Converts the heap [first,last) into a sorted range. The heap property is no longer maintained. # Declarations …

C compatibility

2
  • Finds an element equal to element pointed to by key in an array pointed to by ptr. The array contains count …

  • Sorts the given array pointed to by ptr in ascending order. The array contains count elements of size bytes. …