Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/ranges"

From cppreference.com
< cpp‎ | algorithm
m (adjust)
(Notes: __cpp_lib_default_template_type_for_algorithm_values => __cpp_lib_algorithm_default_value_type)
 
(37 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{title| Constrained algorithms {{mark since c++20}}}}
+
{{title|Constrained algorithms {{mark since c++20}}}}
 
{{cpp/algorithm/ranges/navbar}}
 
{{cpp/algorithm/ranges/navbar}}
  
C++20 provides [[cpp/language/constraints|constrained]] versions of most algorithms in the namespace {{tt|std::ranges}}. In these algorithms, a range can be specified as either a [[cpp/iterator/input_or_output_iterator|iterator]]-[[cpp/iterator/sentinel_for|sentinel]] pair or as a single {{lconcept|Range}} argument, and projections and pointer-to-member callables are supported. Additionally, the return type of most algorithms have been changed to return all potentially useful information computed during the execution of the algorithm.
+
C++20 provides [[cpp/language/constraints|constrained]] versions of most algorithms in the namespace {{tt|std::ranges}}. In these algorithms, a range can be specified as either an [[cpp/iterator/input_or_output_iterator|iterator]]-[[cpp/iterator/sentinel_for|sentinel]] pair or as a single {{lconcept|range}} argument, and projections and pointer-to-member callables are supported. Additionally, the [[cpp/algorithm/ranges#Return types|return types]] of most algorithms have been changed to return all potentially useful information computed during the execution of the algorithm.
  
=== Algorithm concepts and utilities ===
+
===Constrained algorithms===
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc namespace|std::ranges}}
 +
{{dsc h2|Non-modifying sequence operations}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc all_any_none_of}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc for_each}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc for_each_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc count}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc mismatch}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc equal}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc lexicographical_compare}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find_last}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find_end}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find_first_of}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc adjacent_find}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc search}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc search_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc contains}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc starts_with}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc ends_with}}
  
The header {{header|iterator}} provides a set of concepts and related utility templates designed to ease constraining common algorithm operations.
+
{{dsc h2|Modifying sequence operations}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc copy_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc copy_backward}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc move}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc move_backward}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fill}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fill_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc transform}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc generate}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc generate_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc remove}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc remove_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc replace}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc replace_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc swap_ranges}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc reverse}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc reverse_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc rotate}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc rotate_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc shuffle}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc shift}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc sample}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc unique}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc unique_copy}}
  
{{dsc begin}}
+
{{dsc h2|Partitioning operations}}
{{dsc header | iterator}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_partitioned}}
{{dsc namespace | std }}
+
{{dsc inc|cpp/algorithm/ranges/dsc partition}}
{{dsc h2 | Indirect callable concepts}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partition_copy}}
{{dsc inc | cpp/iterator/dsc IndirectUnaryInvocable}}
+
{{dsc inc|cpp/algorithm/ranges/dsc stable_partition}}
{{dsc inc | cpp/iterator/dsc IndirectUnaryPredicate}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partition_point}}
{{dsc inc | cpp/iterator/dsc IndirectRelation}}
+
{{dsc inc | cpp/iterator/dsc IndirectStrictWeakOrder}}
+
{{dsc h2 | Common algorithm requirements}}
+
{{dsc inc | cpp/iterator/dsc IndirectlyMovable}}
+
{{dsc inc | cpp/iterator/dsc IndirectlyMovableStorable}}
+
{{dsc inc | cpp/iterator/dsc IndirectlyCopyable}}
+
{{dsc inc | cpp/iterator/dsc IndirectlyCopyableStorable}}
+
{{dsc inc | cpp/iterator/dsc IndirectlySwappable}}
+
{{dsc inc | cpp/iterator/dsc IndirectlyComparable}}
+
{{dsc inc | cpp/iterator/dsc Permutable}}
+
{{dsc inc | cpp/iterator/dsc Mergeable}}
+
{{dsc inc | cpp/iterator/dsc Sortable}}
+
{{dsc h2 | Utilities}}
+
{{dsc inc | cpp/iterator/dsc indirect_result_t}}
+
{{dsc inc | cpp/iterator/dsc projected}}
+
{{dsc end}}
+
  
=== Constrained algorithms ===
+
{{dsc h2|Sorting operations}}
{{dsc begin}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_sorted}}
{{dsc header | algorithm}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_sorted_until}}
{{dsc namespace | std::ranges}}
+
{{dsc inc|cpp/algorithm/ranges/dsc sort}}
{{dsc h2 | Non-modifying sequence operations}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partial_sort}}
{{dsc inc | cpp/algorithm/ranges/dsc all_any_none_of}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partial_sort_copy}}
{{dsc inc | cpp/algorithm/ranges/dsc for_each}}
+
{{dsc inc|cpp/algorithm/ranges/dsc stable_sort}}
{{dsc inc | cpp/algorithm/ranges/dsc count}}
+
{{dsc inc|cpp/algorithm/ranges/dsc nth_element}}
{{dsc inc | cpp/algorithm/ranges/dsc mismatch}}
+
{{dsc inc | cpp/algorithm/ranges/dsc equal}}
+
{{dsc inc | cpp/algorithm/ranges/dsc lexicographical_compare}}
+
{{dsc inc | cpp/algorithm/ranges/dsc find}}
+
{{dsc inc | cpp/algorithm/ranges/dsc find_end}}
+
{{dsc inc | cpp/algorithm/ranges/dsc find_first_of}}
+
{{dsc inc | cpp/algorithm/ranges/dsc adjacent_find}}
+
{{dsc inc | cpp/algorithm/ranges/dsc search}}
+
{{dsc inc | cpp/algorithm/ranges/dsc search_n}}
+
  
{{dsc h2 | Modifying sequence operations}}
+
{{dsc h2|Binary search operations (on sorted ranges)}}
{{dsc inc | cpp/algorithm/ranges/dsc copy}}
+
{{dsc inc|cpp/algorithm/ranges/dsc lower_bound}}
{{dsc inc | cpp/algorithm/ranges/dsc copy_n}}
+
{{dsc inc|cpp/algorithm/ranges/dsc upper_bound}}
{{dsc inc | cpp/algorithm/ranges/dsc copy_backward}}
+
{{dsc inc|cpp/algorithm/ranges/dsc binary_search}}
{{dsc inc | cpp/algorithm/ranges/dsc move}}
+
{{dsc inc|cpp/algorithm/ranges/dsc equal_range}}
{{dsc inc | cpp/algorithm/ranges/dsc move_backward}}
+
{{dsc inc | cpp/algorithm/ranges/dsc fill}}
+
{{dsc inc | cpp/algorithm/ranges/dsc fill_n}}
+
{{dsc inc | cpp/algorithm/ranges/dsc transform}}
+
{{dsc inc | cpp/algorithm/ranges/dsc generate}}
+
{{dsc inc | cpp/algorithm/ranges/dsc generate_n}}
+
{{dsc inc | cpp/algorithm/ranges/dsc remove}}
+
{{dsc inc | cpp/algorithm/ranges/dsc remove_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc replace}}
+
{{dsc inc | cpp/algorithm/ranges/dsc replace_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc swap_ranges}}
+
{{dsc inc | cpp/algorithm/ranges/dsc reverse}}
+
{{dsc inc | cpp/algorithm/ranges/dsc reverse_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc rotate}}
+
{{dsc inc | cpp/algorithm/ranges/dsc rotate_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc shuffle}}
+
{{dsc inc | cpp/algorithm/ranges/dsc unique}}
+
{{dsc inc | cpp/algorithm/ranges/dsc unique_copy}}
+
  
{{dsc h2 | Partitioning operations}}
+
{{dsc h2|Set operations (on sorted ranges)}}
{{dsc inc | cpp/algorithm/ranges/dsc is_partitioned}}
+
{{dsc inc|cpp/algorithm/ranges/dsc merge}}
{{dsc inc | cpp/algorithm/ranges/dsc partition}}
+
{{dsc inc|cpp/algorithm/ranges/dsc inplace_merge}}
{{dsc inc | cpp/algorithm/ranges/dsc partition_copy}}
+
{{dsc inc|cpp/algorithm/ranges/dsc includes}}
{{dsc inc | cpp/algorithm/ranges/dsc stable_partition}}
+
{{dsc inc|cpp/algorithm/ranges/dsc set_difference}}
{{dsc inc | cpp/algorithm/ranges/dsc partition_point}}
+
{{dsc inc|cpp/algorithm/ranges/dsc set_intersection}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc set_symmetric_difference}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc set_union}}
  
{{dsc h2 | Sorting operations}}
+
{{dsc h2|Heap operations}}
{{dsc inc | cpp/algorithm/ranges/dsc is_sorted}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc is_sorted_until}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_heap_until}}
{{dsc inc | cpp/algorithm/ranges/dsc sort}}
+
{{dsc inc|cpp/algorithm/ranges/dsc make_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc partial_sort}}
+
{{dsc inc|cpp/algorithm/ranges/dsc push_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc partial_sort_copy}}
+
{{dsc inc|cpp/algorithm/ranges/dsc pop_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc stable_sort}}
+
{{dsc inc|cpp/algorithm/ranges/dsc sort_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc nth_element}}
+
  
{{dsc h2 | Binary search operations (on sorted ranges)}}
+
{{dsc h2|Minimum/maximum operations}}
{{dsc inc | cpp/algorithm/ranges/dsc lower_bound}}
+
{{dsc inc|cpp/algorithm/ranges/dsc max}}
{{dsc inc | cpp/algorithm/ranges/dsc upper_bound}}
+
{{dsc inc|cpp/algorithm/ranges/dsc max_element}}
{{dsc inc | cpp/algorithm/ranges/dsc binary_search}}
+
{{dsc inc|cpp/algorithm/ranges/dsc min}}
{{dsc inc | cpp/algorithm/ranges/dsc equal_range}}
+
{{dsc inc|cpp/algorithm/ranges/dsc min_element}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc minmax}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc minmax_element}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc clamp}}
  
{{dsc h2 | Set operations (on sorted ranges)}}
+
{{dsc h2|Permutation operations}}
{{dsc inc | cpp/algorithm/ranges/dsc merge}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_permutation}}
{{dsc inc | cpp/algorithm/ranges/dsc inplace_merge}}
+
{{dsc inc|cpp/algorithm/ranges/dsc next_permutation}}
{{dsc inc | cpp/algorithm/ranges/dsc includes}}
+
{{dsc inc|cpp/algorithm/ranges/dsc prev_permutation}}
{{dsc inc | cpp/algorithm/ranges/dsc set_difference}}
+
{{dsc end}}
{{dsc inc | cpp/algorithm/ranges/dsc set_intersection}}
+
{{dsc inc | cpp/algorithm/ranges/dsc set_symmetric_difference}}
+
{{dsc inc | cpp/algorithm/ranges/dsc set_union}}
+
  
{{dsc h2 | Heap operations}}
+
===Constrained numeric operations===
{{dsc inc | cpp/algorithm/ranges/dsc is_heap}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/ranges/dsc is_heap_until}}
+
{{dsc header|numeric}}
{{dsc inc | cpp/algorithm/ranges/dsc make_heap}}
+
{{dsc namespace|std::ranges}}
{{dsc inc | cpp/algorithm/ranges/dsc push_heap}}
+
{{dsc inc|cpp/algorithm/ranges/dsc iota}}
{{dsc inc | cpp/algorithm/ranges/dsc pop_heap}}
+
{{dsc end}}
{{dsc inc | cpp/algorithm/ranges/dsc sort_heap}}
+
  
{{dsc h2 | Minimum/maximum operations}}
+
===Constrained fold operations===
{{dsc inc | cpp/algorithm/ranges/dsc max}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/ranges/dsc max_element}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/algorithm/ranges/dsc min}}
+
{{dsc namespace|std::ranges}}
{{dsc inc | cpp/algorithm/ranges/dsc min_element}}
+
{{dsc inc|cpp/algorithm/ranges/dsc fold_left}}
{{dsc inc | cpp/algorithm/ranges/dsc minmax}}
+
{{dsc inc|cpp/algorithm/ranges/dsc fold_left_first}}
{{dsc inc | cpp/algorithm/ranges/dsc minmax_element}}
+
{{dsc inc|cpp/algorithm/ranges/dsc fold_right}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fold_right_last}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fold_left_with_iter}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fold_left_first_with_iter}}
 +
<!--
 +
{{dsc inc|cpp/algorithm/ranges/dsc accumulate}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc inner_product}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc adjacent_difference}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc partial_sum}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc reduce}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc exclusive_scan}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc inclusive_scan}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc transform_reduce}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc transform_exclusive_scan}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc transform_inclusive_scan}}
 +
-->
 +
{{dsc end}}
  
{{dsc h2 | Permutation operations}}
+
===Constrained uninitialized memory algorithms===
{{dsc inc | cpp/algorithm/ranges/dsc is_permutation}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/ranges/dsc next_permutation}}
+
{{dsc header|memory}}
{{dsc inc | cpp/algorithm/ranges/dsc prev_permutation}}
+
{{dsc namespace|std::ranges}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_copy}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_copy_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_fill}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_fill_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_move}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_move_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_default_construct}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_default_construct_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_value_construct}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_value_construct_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc destroy}}
 +
{{dsc inc|cpp/memory/ranges/dsc destroy_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc destroy_at}}
 +
{{dsc inc|cpp/memory/ranges/dsc construct_at}}
 
{{dsc end}}
 
{{dsc end}}
  
===Constrained uninitialized memory algorithms ===
+
===Constrained random number algorithms===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | memory}}
+
{{dsc header|random}}
{{dsc namespace | std::ranges}}
+
{{dsc namespace|std::ranges}}
{{dsc inc | cpp/memory/ranges/dsc uninitialized_copy}}
+
{{dsc inc|cpp/algorithm/ranges/dsc generate_random}}
{{dsc inc | cpp/memory/ranges/dsc uninitialized_copy_n}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_fill}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_fill_n}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_move}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_move_n}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_default_construct}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_default_construct_n}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_value_construct}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_value_construct_n}}
+
{{dsc inc | cpp/memory/ranges/dsc destroy_at}}
+
{{dsc inc | cpp/memory/ranges/dsc destroy}}
+
{{dsc inc | cpp/memory/ranges/dsc destroy_n}}
+
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
===Return types===
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc namespace|std::ranges}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc in_fun_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc in_in_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc in_out_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc in_in_out_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc in_out_out_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc min_max_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc in_found_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc in_value_result}}
 +
{{dsc inc|cpp/algorithm/ranges/return_types/dsc out_value_result}}
 +
{{dsc end}}
 +
 
 +
===Notes===
 +
{{ftm begin|std=1|sort=1|comment=1}}
 +
{{ftm|std=C++26|value=202403L|__cpp_lib_algorithm_default_value_type|[[cpp/language/list_initialization|List-initialization]] for algorithms}}
 +
{{ftm|std=C++20|value=201911L|__cpp_lib_ranges|[[cpp/ranges|Ranges library]] and [[#Top|constrained algorithms]]}}
 +
{{ftm|std=C++23|value=202207L|__cpp_lib_ranges_contains|{{lc|std::ranges::contains}}}}
 +
{{ftm|std=C++23|value=202207L|__cpp_lib_ranges_find_last|{{lc|std::ranges::find_last}}}}
 +
{{ftm|std=C++23|value=202207L|__cpp_lib_ranges_fold|{{tt|std::ranges}} [[#Constrained fold operations|fold algorithms]]}}
 +
{{ftm|std=C++23|value=202202L|__cpp_lib_ranges_iota|{{lc|std::ranges::iota}}}}
 +
{{ftm|std=C++23|value=202106L|__cpp_lib_ranges_starts_ends_with|{{lc|std::ranges::starts_with}}, {{lc|std::ranges::ends_with}}}}
 +
{{ftm|std=C++20|value=201806L|rowspan="2"|__cpp_lib_shift|{{lc|std::shift_left}}, {{lc|std::shift_right}}}}
 +
{{ftm|std=C++23|value=202202L|-|{{lc|std::ranges::shift_left}}, {{lc|std::ranges::shift_right}}}}
 +
{{ftm|std=C++26|value=202403L|__cpp_lib_ranges_generate_random|{{lc|std::ranges::generate_random}}}}
 +
{{ftm end}}
 +
 
 +
{{langlinks|es|ja|ru|zh}}

Latest revision as of 09:31, 20 May 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Fold operations
Numeric operations
(C++23)            
Operations on uninitialized storage
Return types
 

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.

Contents

[edit] Constrained algorithms

Defined in header <algorithm>
Defined in namespace std::ranges
Non-modifying sequence operations
checks if a predicate is true for all, any or none of the elements in a range
(niebloid)[edit]
applies a function to a range of elements
(niebloid)[edit]
applies a function object to the first N elements of a sequence
(niebloid)[edit]
returns the number of elements satisfying specific criteria
(niebloid)[edit]
finds the first position where two ranges differ
(niebloid)[edit]
determines if two sets of elements are the same
(niebloid)[edit]
returns true if one range is lexicographically less than another
(niebloid)[edit]
finds the first element satisfying specific criteria
(niebloid)[edit]
finds the last element satisfying specific criteria
(niebloid)[edit]
finds the last sequence of elements in a certain range
(niebloid)[edit]
searches for any one of a set of elements
(niebloid)[edit]
finds the first two adjacent items that are equal (or satisfy a given predicate)
(niebloid)[edit]
searches for the first occurrence of a range of elements
(niebloid)[edit]
searches for the first occurrence of a number consecutive copies of an element in a range
(niebloid)[edit]
checks if the range contains the given element or subrange
(niebloid)[edit]
checks whether a range starts with another range
(niebloid)[edit]
checks whether a range ends with another range
(niebloid)[edit]
Modifying sequence operations
copies a range of elements to a new location
(niebloid)[edit]
copies a number of elements to a new location
(niebloid)[edit]
copies a range of elements in backwards order
(niebloid)[edit]
moves a range of elements to a new location
(niebloid)[edit]
moves a range of elements to a new location in backwards order
(niebloid)[edit]
assigns a range of elements a certain value
(niebloid)[edit]
assigns a value to a number of elements
(niebloid)[edit]
applies a function to a range of elements
(niebloid)[edit]
saves the result of a function in a range
(niebloid)[edit]
saves the result of N applications of a function
(niebloid)[edit]
removes elements satisfying specific criteria
(niebloid)[edit]
copies a range of elements omitting those that satisfy specific criteria
(niebloid)[edit]
replaces all values satisfying specific criteria with another value
(niebloid)[edit]
copies a range, replacing elements satisfying specific criteria with another value
(niebloid)[edit]
swaps two ranges of elements
(niebloid)[edit]
reverses the order of elements in a range
(niebloid)[edit]
creates a copy of a range that is reversed
(niebloid)[edit]
rotates the order of elements in a range
(niebloid)[edit]
copies and rotate a range of elements
(niebloid)[edit]
randomly re-orders elements in a range
(niebloid)[edit]
shifts elements in a range
(niebloid)[edit]
selects N random elements from a sequence
(niebloid)[edit]
removes consecutive duplicate elements in a range
(niebloid)[edit]
creates a copy of some range of elements that contains no consecutive duplicates
(niebloid)[edit]
Partitioning operations
determines if the range is partitioned by the given predicate
(niebloid)[edit]
divides a range of elements into two groups
(niebloid)[edit]
copies a range dividing the elements into two groups
(niebloid)[edit]
divides elements into two groups while preserving their relative order
(niebloid)[edit]
locates the partition point of a partitioned range
(niebloid)[edit]
Sorting operations
checks whether a range is sorted into ascending order
(niebloid)[edit]
finds the largest sorted subrange
(niebloid)[edit]
sorts a range into ascending order
(niebloid)[edit]
sorts the first N elements of a range
(niebloid)[edit]
copies and partially sorts a range of elements
(niebloid)[edit]
sorts a range of elements while preserving order between equal elements
(niebloid)[edit]
partially sorts the given range making sure that it is partitioned by the given element
(niebloid)[edit]
Binary search operations (on sorted ranges)
returns an iterator to the first element not less than the given value
(niebloid)[edit]
returns an iterator to the first element greater than a certain value
(niebloid)[edit]
determines if an element exists in a partially-ordered range
(niebloid)[edit]
returns range of elements matching a specific key
(niebloid)[edit]
Set operations (on sorted ranges)
merges two sorted ranges
(niebloid)[edit]
merges two ordered ranges in-place
(niebloid)[edit]
returns true if one sequence is a subsequence of another
(niebloid)[edit]
computes the difference between two sets
(niebloid)[edit]
computes the intersection of two sets
(niebloid)[edit]
computes the symmetric difference between two sets
(niebloid)[edit]
computes the union of two sets
(niebloid)[edit]
Heap operations
checks if the given range is a max heap
(niebloid)[edit]
finds the largest subrange that is a max heap
(niebloid)[edit]
creates a max heap out of a range of elements
(niebloid)[edit]
adds an element to a max heap
(niebloid)[edit]
removes the largest element from a max heap
(niebloid)[edit]
turns a max heap into a range of elements sorted in ascending order
(niebloid)[edit]
Minimum/maximum operations
returns the greater of the given values
(niebloid)[edit]
returns the largest element in a range
(niebloid)[edit]
returns the smaller of the given values
(niebloid)[edit]
returns the smallest element in a range
(niebloid)[edit]
returns the smaller and larger of two elements
(niebloid)[edit]
returns the smallest and the largest elements in a range
(niebloid)[edit]
clamps a value between a pair of boundary values
(niebloid)[edit]
Permutation operations
determines if a sequence is a permutation of another sequence
(niebloid)[edit]
generates the next greater lexicographic permutation of a range of elements
(niebloid)[edit]
generates the next smaller lexicographic permutation of a range of elements
(niebloid)[edit]

[edit] Constrained numeric operations

Defined in header <numeric>
Defined in namespace std::ranges
fills a range with successive increments of the starting value
(niebloid)[edit]

[edit] Constrained fold operations

Defined in header <algorithm>
Defined in namespace std::ranges
left-folds a range of elements
(niebloid)[edit]
left-folds a range of elements using the first element as an initial value
(niebloid)[edit]
right-folds a range of elements
(niebloid)[edit]
right-folds a range of elements using the last element as an initial value
(niebloid)[edit]
left-folds a range of elements, and returns a pair (iterator, value)
(niebloid)[edit]
left-folds a range of elements using the first element as an initial value, and returns a pair (iterator, optional)
(niebloid)[edit]

[edit] Constrained uninitialized memory algorithms

Defined in header <memory>
Defined in namespace std::ranges
copies a range of objects to an uninitialized area of memory
(niebloid)[edit]
copies a number of objects to an uninitialized area of memory
(niebloid)[edit]
copies an object to an uninitialized area of memory, defined by a range
(niebloid)[edit]
copies an object to an uninitialized area of memory, defined by a start and a count
(niebloid)[edit]
moves a range of objects to an uninitialized area of memory
(niebloid)[edit]
moves a number of objects to an uninitialized area of memory
(niebloid)[edit]
constructs objects by default-initialization in an uninitialized area of memory, defined by a range
(niebloid)[edit]
constructs objects by default-initialization in an uninitialized area of memory, defined by a start and count
(niebloid)[edit]
constructs objects by value-initialization in an uninitialized area of memory, defined by a range
(niebloid)[edit]
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
(niebloid)[edit]
destroys a range of objects
(niebloid)[edit]
destroys a number of objects in a range
(niebloid)[edit]
destroys an object at a given address
(niebloid)[edit]
creates an object at a given address
(niebloid)[edit]

[edit] Constrained random number algorithms

Defined in header <random>
Defined in namespace std::ranges
fills a range with random numbers from a uniform random bit generator
(niebloid)[edit]

[edit] Return types

Defined in header <algorithm>
Defined in namespace std::ranges
provides a way to store an iterator and a function object as a single unit
(class template) [edit]
provides a way to store two iterators as a single unit
(class template) [edit]
provides a way to store two iterators as a single unit
(class template) [edit]
provides a way to store three iterators as a single unit
(class template) [edit]
provides a way to store three iterators as a single unit
(class template) [edit]
provides a way to store two objects or references of the same type as a single unit
(class template) [edit]
provides a way to store an iterator and a boolean flag as a single unit
(class template) [edit]
provides a way to store an iterator and a value as a single unit
(class template) [edit]
provides a way to store an iterator and a value as a single unit
(class template) [edit]

[edit] 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