Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm"

From cppreference.com
< cpp
(Undo revision 138584 by 61.2.194.101 (talk))
(Notes: __cpp_lib_default_template_type_for_algorithm_values => __cpp_lib_algorithm_default_value_type)
 
(28 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{title | Algorithms library}}
+
{{title|Algorithms library}}
{{cpp/algorithm/ranges/navbar}}
+
{{cpp/algorithm/navbar}}
  
The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Note that a range is defined as {{tt|[first, last)}} where {{tt|last}} refers to the element ''past'' the last element to inspect or modify.
+
The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Note that a range is defined as {{range|first|last}} where {{c|last}} refers to the element ''past'' the last element to inspect or modify.
  
 
{{rrev|since=c++20|
 
{{rrev|since=c++20|
===== {{rl|ranges|Constrained algorithms}} =====
+
==={{rl|ranges|Constrained algorithms}}===
 
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.
 
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.
 
{{source|1=
 
{{source|1=
std::vector<int> v = {7, 1, 4, 0, -1};
+
std::vector<int> v {7, 1, 4, 0, -1};
 
std::ranges::sort(v); // constrained algorithm
 
std::ranges::sort(v); // constrained algorithm
 
}}
 
}}
Line 15: Line 15:
  
 
{{rrev|since=c++17|
 
{{rrev|since=c++17|
=====Execution policies=====
+
===Execution policies===
 
Most algorithms have overloads that accept execution policies. The standard library algorithms support several [[cpp/algorithm/execution_policy_tag_t|execution policies]], and the library provides corresponding execution policy types and objects. Users may select an execution policy statically by invoking a parallel algorithm with an [[cpp/algorithm/execution_policy_tag|execution policy object]] of the corresponding type.
 
Most algorithms have overloads that accept execution policies. The standard library algorithms support several [[cpp/algorithm/execution_policy_tag_t|execution policies]], and the library provides corresponding execution policy types and objects. Users may select an execution policy statically by invoking a parallel algorithm with an [[cpp/algorithm/execution_policy_tag|execution policy object]] of the corresponding type.
  
Line 23: Line 23:
  
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | execution }}
+
{{dsc header|execution}}
{{dsc namespace | std::execution }}
+
{{dsc namespace|std::execution}}
{{dsc inc | cpp/algorithm/dsc execution_policy_tag_t }}
+
{{dsc inc|cpp/algorithm/dsc execution_policy_tag_t}}
{{dsc inc | cpp/algorithm/dsc execution_policy_tag }}
+
{{dsc inc|cpp/algorithm/dsc execution_policy_tag}}
{{dsc namespace | std }}
+
{{dsc namespace|std}}
{{dsc inc | cpp/algorithm/dsc is_execution_policy }}
+
{{dsc inc|cpp/algorithm/dsc is_execution_policy}}
 
{{dsc end}}
 
{{dsc end}}
 +
{{ftm begin|std=1|comment=1}}
 +
{{ftm|__cpp_lib_parallel_algorithm|Parallel algorithms|std=C++17|value=201603L}}
 +
{{ftm|__cpp_lib_execution|rowspan=2|Execution policies|std=C++17|value=201603L}}
 +
{{ftm|-|{{lc|std::execution::unsequenced_policy}}|std=C++20|value=201902L}}
 +
{{ftm end}}
 +
}}
  
 +
===Non-modifying sequence operations===
 +
====Batch operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc for_each}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc for_each}}
 +
{{dsc inc|cpp/algorithm/dsc for_each_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc for_each_n}}
 +
{{dsc end}}
  
{{feature test macro|__cpp_lib_parallel_algorithm}} (for parallel version of algorithms).
+
====Search operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc all_any_none_of}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc all_any_none_of}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc contains}}
 +
{{dsc inc|cpp/algorithm/dsc find}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find_last}}
 +
{{dsc inc|cpp/algorithm/dsc find_end}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find_end}}
 +
{{dsc inc|cpp/algorithm/dsc find_first_of}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc find_first_of}}
 +
{{dsc inc|cpp/algorithm/dsc adjacent_find}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc adjacent_find}}
 +
{{dsc inc|cpp/algorithm/dsc count}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc count}}
 +
{{dsc inc|cpp/algorithm/dsc mismatch}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc mismatch}}
 +
{{dsc inc|cpp/algorithm/dsc equal}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc equal}}
 +
{{dsc inc|cpp/algorithm/dsc search}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc search}}
 +
{{dsc inc|cpp/algorithm/dsc search_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc search_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc starts_with}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc ends_with}}
 +
{{dsc end}}
  
{{feature test macro|__cpp_lib_execution}} (for execution policies).
+
====Fold operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fold_left}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fold_left_first}}
 +
{{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 end}}
 +
 
 +
===Modifying sequence operations===
 +
====Copy operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc copy}}
 +
{{dsc inc|cpp/algorithm/dsc copy_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc copy_n}}
 +
{{dsc inc|cpp/algorithm/dsc copy_backward}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc copy_backward}}
 +
{{dsc inc|cpp/algorithm/dsc move}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc move}}
 +
{{dsc inc|cpp/algorithm/dsc move_backward}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc move_backward}}
 +
{{dsc end}}
 +
 
 +
====Swap operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm|{{nbspt|3}}{{mark until c++11}}}}
 +
{{dsc header|utility|{{nbspt|5}}{{mark since c++11}}}}
 +
{{dsc header|string_view}}
 +
{{dsc inc|cpp/algorithm/dsc swap}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc swap_ranges}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc swap_ranges}}
 +
{{dsc inc|cpp/algorithm/dsc iter_swap}}
 +
{{dsc end}}
 +
 
 +
====Transformation operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc transform}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc transform}}
 +
{{dsc inc|cpp/algorithm/dsc replace}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc replace}}
 +
{{dsc inc|cpp/algorithm/dsc replace_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc replace_copy}}
 +
{{dsc end}}
 +
 
 +
====Generation operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc fill}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fill}}
 +
{{dsc inc|cpp/algorithm/dsc fill_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc fill_n}}
 +
{{dsc inc|cpp/algorithm/dsc generate}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc generate}}
 +
{{dsc inc|cpp/algorithm/dsc generate_n}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc generate_n}}
 +
{{dsc end}}
 +
 
 +
====Removing operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc remove}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc remove}}
 +
{{dsc inc|cpp/algorithm/dsc remove_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc remove_copy}}
 +
{{dsc inc|cpp/algorithm/dsc unique}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc unique}}
 +
{{dsc inc|cpp/algorithm/dsc unique_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc unique_copy}}
 +
{{dsc end}}
 +
 
 +
====Order-changing operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc reverse}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc reverse}}
 +
{{dsc inc|cpp/algorithm/dsc reverse_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc reverse_copy}}
 +
{{dsc inc|cpp/algorithm/dsc rotate}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc rotate}}
 +
{{dsc inc|cpp/algorithm/dsc rotate_copy}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc rotate_copy}}
 +
{{dsc inc|cpp/algorithm/dsc shift}}
 +
{{dsc inc|cpp/algorithm/dsc random_shuffle}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc shuffle}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc shift}}
 +
{{dsc end}}
 +
 
 +
====Sampling operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc sample}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc sample}}
 +
{{dsc end}}
 +
 
 +
===Sorting and related operations===
 +
====Requirements====
 +
Some algorithms require the sequence represented by the arguments to be “sorted” or “partitioned”. The behavior is undefined if the requirement is not met.
 +
 
 +
{{rev begin}}
 +
{{rev|until=c++20|
 +
A sequence is ''sorted with respect to a comparator {{c|comp}}'' if for every iterator {{c|iter}} pointing to the sequence and every non-negative integer {{c|n}} such that {{c|iter + n}}<ref name="plus">{{c|iter + n}} simply means “the result of {{c|iter}} being incremented {{c|n}} times”, regardless of whether {{c|iter}} is a random access iterator.</ref> is a {{rlp|iterator#Dereferenceability and validity|valid iterator}} pointing to an element of the sequence, {{c|1=comp(*(iter + n), *iter) == false}}<ref name="plus" />.
 
}}
 
}}
 +
{{rev|since=c++20|
 +
A sequence is ''sorted with respect to {{c|comp}} and {{c|proj}}'' for a comparator {{c|comp}} and projection {{c|proj}} if for every iterator {{c|iter}} pointing to the sequence and every non-negative integer {{c|n}} such that {{c|iter + n}}<ref name="plus" /> is a valid iterator pointing to an element of the sequence, {{c multi|bool(std::invoke(comp, std::invoke(proj, *(iter + n)),|                      std::invoke(proj, *iter)))}}<ref name="plus" /> is {{c|false}}.
 +
 +
A sequence is ''sorted with respect to a comparator {{c|comp}}'' if the sequence is sorted with respect to {{c|comp}} and {{c|std::identity{}<!---->}} (the identity projection).
 +
}}
 +
{{rev end}}
 +
 +
A sequence {{range|start|finish}} is ''partitioned with respect to an expression {{c|f(e)}}'' if there exists an integer {{c|n}} such that for all {{c|i}} in {{range|0|std::distance(start, finish)}}, {{c|f(*(start + i))}}<ref name="plus" /> is {{c|true}} if and only if {{c|i < n}}.
 +
 +
<references/>
  
 +
====Partitioning operations====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc h2 | Non-modifying sequence operations}}
+
{{dsc header|algorithm}}
{{dsc header | algorithm}}
+
{{dsc inc|cpp/algorithm/dsc is_partitioned}}
{{dsc inc | cpp/algorithm/dsc all_any_none_of}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_partitioned}}
{{dsc inc | cpp/algorithm/ranges/dsc all_any_none_of}}
+
{{dsc inc|cpp/algorithm/dsc partition}}
{{dsc inc | cpp/algorithm/dsc for_each}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partition}}
{{dsc inc | cpp/algorithm/ranges/dsc for_each}}
+
{{dsc inc|cpp/algorithm/dsc partition_copy}}
{{dsc inc | cpp/algorithm/dsc for_each_n}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partition_copy}}
{{dsc inc | cpp/algorithm/ranges/dsc for_each_n}}
+
{{dsc inc|cpp/algorithm/dsc stable_partition}}
{{dsc inc | cpp/algorithm/dsc count}}
+
{{dsc inc|cpp/algorithm/ranges/dsc stable_partition}}
{{dsc inc | cpp/algorithm/ranges/dsc count}}
+
{{dsc inc|cpp/algorithm/dsc partition_point}}
{{dsc inc | cpp/algorithm/dsc mismatch}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partition_point}}
{{dsc inc | cpp/algorithm/ranges/dsc mismatch}}
+
{{dsc end}}
{{dsc inc | cpp/algorithm/dsc find}}
+
{{dsc inc | cpp/algorithm/ranges/dsc find}}
+
{{dsc inc | cpp/algorithm/dsc find_end}}
+
{{dsc inc | cpp/algorithm/ranges/dsc find_end}}
+
{{dsc inc | cpp/algorithm/dsc find_first_of}}
+
{{dsc inc | cpp/algorithm/ranges/dsc find_first_of}}
+
{{dsc inc | cpp/algorithm/dsc adjacent_find}}
+
{{dsc inc | cpp/algorithm/ranges/dsc adjacent_find}}
+
{{dsc inc | cpp/algorithm/dsc search}}
+
{{dsc inc | cpp/algorithm/ranges/dsc search}}
+
{{dsc inc | cpp/algorithm/dsc search_n}}
+
{{dsc inc | cpp/algorithm/ranges/dsc search_n}}
+
{{dsc inc | cpp/algorithm/ranges/dsc starts_with}}
+
{{dsc inc | cpp/algorithm/ranges/dsc ends_with}}
+
  
{{dsc h2 | Modifying sequence operations}}
+
====Sorting operations====
{{dsc header | algorithm}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc copy}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/algorithm/ranges/dsc copy}}
+
{{dsc inc|cpp/algorithm/dsc sort}}
{{dsc inc | cpp/algorithm/dsc copy_n}}
+
{{dsc inc|cpp/algorithm/ranges/dsc sort}}
{{dsc inc | cpp/algorithm/ranges/dsc copy_n}}
+
{{dsc inc|cpp/algorithm/dsc stable_sort}}
{{dsc inc | cpp/algorithm/dsc copy_backward}}
+
{{dsc inc|cpp/algorithm/ranges/dsc stable_sort}}
{{dsc inc | cpp/algorithm/ranges/dsc copy_backward}}
+
{{dsc inc|cpp/algorithm/dsc partial_sort}}
{{dsc inc | cpp/algorithm/dsc move}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partial_sort}}
{{dsc inc | cpp/algorithm/ranges/dsc move}}
+
{{dsc inc|cpp/algorithm/dsc partial_sort_copy}}
{{dsc inc | cpp/algorithm/dsc move_backward}}
+
{{dsc inc|cpp/algorithm/ranges/dsc partial_sort_copy}}
{{dsc inc | cpp/algorithm/ranges/dsc move_backward}}
+
{{dsc inc|cpp/algorithm/dsc is_sorted}}
{{dsc inc | cpp/algorithm/dsc fill}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_sorted}}
{{dsc inc | cpp/algorithm/ranges/dsc fill}}
+
{{dsc inc|cpp/algorithm/dsc is_sorted_until}}
{{dsc inc | cpp/algorithm/dsc fill_n}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_sorted_until}}
{{dsc inc | cpp/algorithm/ranges/dsc fill_n}}
+
{{dsc inc|cpp/algorithm/dsc nth_element}}
{{dsc inc | cpp/algorithm/dsc transform}}
+
{{dsc inc|cpp/algorithm/ranges/dsc nth_element}}
{{dsc inc | cpp/algorithm/ranges/dsc transform}}
+
{{dsc end}}
{{dsc inc | cpp/algorithm/dsc generate}}
+
{{dsc inc | cpp/algorithm/ranges/dsc generate}}
+
{{dsc inc | cpp/algorithm/dsc generate_n}}
+
{{dsc inc | cpp/algorithm/ranges/dsc generate_n}}
+
{{dsc inc | cpp/algorithm/dsc remove}}
+
{{dsc inc | cpp/algorithm/ranges/dsc remove}}
+
{{dsc inc | cpp/algorithm/dsc remove_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc remove_copy}}
+
{{dsc inc | cpp/algorithm/dsc replace}}
+
{{dsc inc | cpp/algorithm/ranges/dsc replace}}
+
{{dsc inc | cpp/algorithm/dsc replace_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc replace_copy}}
+
{{dsc inc | cpp/algorithm/dsc swap}}
+
{{dsc inc | cpp/algorithm/dsc swap_ranges}}
+
{{dsc inc | cpp/algorithm/ranges/dsc swap_ranges}}
+
{{dsc inc | cpp/algorithm/dsc iter_swap}}
+
{{dsc inc | cpp/algorithm/dsc reverse}}
+
{{dsc inc | cpp/algorithm/ranges/dsc reverse}}
+
{{dsc inc | cpp/algorithm/dsc reverse_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc reverse_copy}}
+
{{dsc inc | cpp/algorithm/dsc rotate}}
+
{{dsc inc | cpp/algorithm/ranges/dsc rotate}}
+
{{dsc inc | cpp/algorithm/dsc rotate_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc rotate_copy}}
+
{{dsc inc | cpp/algorithm/dsc shift}}
+
{{dsc inc | cpp/algorithm/ranges/dsc shift}}
+
{{dsc inc | cpp/algorithm/dsc random_shuffle}}
+
{{dsc inc | cpp/algorithm/ranges/dsc shuffle}}
+
{{dsc inc | cpp/algorithm/dsc sample}}
+
{{dsc inc | cpp/algorithm/ranges/dsc sample}}
+
{{dsc inc | cpp/algorithm/dsc unique}}
+
{{dsc inc | cpp/algorithm/ranges/dsc unique}}
+
{{dsc inc | cpp/algorithm/dsc unique_copy}}
+
{{dsc inc | cpp/algorithm/ranges/dsc unique_copy}}
+
  
{{dsc h2 | Partitioning operations}}
+
====Binary search operations (on partitioned ranges)====
{{dsc header | algorithm}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc is_partitioned}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/algorithm/ranges/dsc is_partitioned}}
+
{{dsc inc|cpp/algorithm/dsc lower_bound}}
{{dsc inc | cpp/algorithm/dsc partition}}
+
{{dsc inc|cpp/algorithm/ranges/dsc lower_bound}}
{{dsc inc | cpp/algorithm/ranges/dsc partition}}
+
{{dsc inc|cpp/algorithm/dsc upper_bound}}
{{dsc inc | cpp/algorithm/dsc partition_copy}}
+
{{dsc inc|cpp/algorithm/ranges/dsc upper_bound}}
{{dsc inc | cpp/algorithm/ranges/dsc partition_copy}}
+
{{dsc inc|cpp/algorithm/dsc equal_range}}
{{dsc inc | cpp/algorithm/dsc stable_partition}}
+
{{dsc inc|cpp/algorithm/ranges/dsc equal_range}}
{{dsc inc | cpp/algorithm/ranges/dsc stable_partition}}
+
{{dsc inc|cpp/algorithm/dsc binary_search}}
{{dsc inc | cpp/algorithm/dsc partition_point}}
+
{{dsc inc|cpp/algorithm/ranges/dsc binary_search}}
{{dsc inc | cpp/algorithm/ranges/dsc partition_point}}
+
{{dsc end}}
  
{{dsc h2 | Sorting operations}}
+
====Set operations (on sorted ranges)====
{{dsc header | algorithm}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc is_sorted}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/algorithm/ranges/dsc is_sorted}}
+
{{dsc inc|cpp/algorithm/dsc includes}}
{{dsc inc | cpp/algorithm/dsc is_sorted_until}}
+
{{dsc inc|cpp/algorithm/ranges/dsc includes}}
{{dsc inc | cpp/algorithm/ranges/dsc is_sorted_until}}
+
{{dsc inc|cpp/algorithm/dsc set_union}}
{{dsc inc | cpp/algorithm/dsc sort}}
+
{{dsc inc|cpp/algorithm/ranges/dsc set_union}}
{{dsc inc | cpp/algorithm/ranges/dsc sort}}
+
{{dsc inc|cpp/algorithm/dsc set_intersection}}
{{dsc inc | cpp/algorithm/dsc partial_sort}}
+
{{dsc inc|cpp/algorithm/ranges/dsc set_intersection}}
{{dsc inc | cpp/algorithm/ranges/dsc partial_sort}}
+
{{dsc inc|cpp/algorithm/dsc set_difference}}
{{dsc inc | cpp/algorithm/dsc partial_sort_copy}}
+
{{dsc inc|cpp/algorithm/ranges/dsc set_difference}}
{{dsc inc | cpp/algorithm/ranges/dsc partial_sort_copy}}
+
{{dsc inc|cpp/algorithm/dsc set_symmetric_difference}}
{{dsc inc | cpp/algorithm/dsc stable_sort}}
+
{{dsc inc|cpp/algorithm/ranges/dsc set_symmetric_difference}}
{{dsc inc | cpp/algorithm/ranges/dsc stable_sort}}
+
{{dsc end}}
{{dsc inc | cpp/algorithm/dsc nth_element}}
+
{{dsc inc | cpp/algorithm/ranges/dsc nth_element}}
+
  
{{dsc h2 | Binary search operations (on sorted ranges)}}
+
====Merge operations (on sorted ranges)====
{{dsc header | algorithm}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc lower_bound}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/algorithm/ranges/dsc lower_bound}}
+
{{dsc inc|cpp/algorithm/dsc merge}}
{{dsc inc | cpp/algorithm/dsc upper_bound}}
+
{{dsc inc|cpp/algorithm/ranges/dsc merge}}
{{dsc inc | cpp/algorithm/ranges/dsc upper_bound}}
+
{{dsc inc|cpp/algorithm/dsc inplace_merge}}
{{dsc inc | cpp/algorithm/dsc binary_search}}
+
{{dsc inc|cpp/algorithm/ranges/dsc inplace_merge}}
{{dsc inc | cpp/algorithm/ranges/dsc binary_search}}
+
{{dsc end}}
{{dsc inc | cpp/algorithm/dsc equal_range}}
+
{{dsc inc | cpp/algorithm/ranges/dsc equal_range}}
+
  
{{dsc h2 | Other operations on sorted ranges}}
+
====Heap operations====
{{dsc header | algorithm}}
+
{{rev begin}}
{{dsc inc | cpp/algorithm/dsc merge}}
+
{{rev|until=c++20|
{{dsc inc | cpp/algorithm/ranges/dsc merge}}
+
A random access [[cpp/iterator#Ranges|range]] {{range|first|last}} is a ''heap with respect to a comparator {{c|comp}}'' if {{c|bool(comp(first[(i - 1) / 2], first[i]))}} is {{c|false}} for all integer {{c|i}} in {{open range|0|last - first}}.
{{dsc inc | cpp/algorithm/dsc inplace_merge}}
+
}}
{{dsc inc | cpp/algorithm/ranges/dsc inplace_merge}}
+
{{rev|since=c++20|
 +
A random access [[cpp/iterator#Ranges|range]] {{range|first|last}} is a ''heap with respect to {{c|comp}} and {{c|proj}}'' for a comparator {{c|comp}} and projection {{c|proj}} if {{c multi|bool(std::invoke(comp, std::invoke(proj, first[(i - 1) / 2]),|                      std::invoke(proj, first[i]))}} is {{c|false}} for all integer {{c|i}} in {{open range|0|last - first}}.
  
{{dsc h2 | Set operations (on sorted ranges)}}
+
A random access range {{range|first|last}} is a ''heap with respect to a comparator {{c|comp}}'' if the range is a heap with respect to {{c|comp}} and {{c|std::identity{}<!---->}} (the identity projection).
{{dsc header | algorithm}}
+
}}
{{dsc inc | cpp/algorithm/dsc includes}}
+
{{rev end}}
{{dsc inc | cpp/algorithm/ranges/dsc includes}}
+
{{dsc inc | cpp/algorithm/dsc set_difference}}
+
{{dsc inc | cpp/algorithm/ranges/dsc set_difference}}
+
{{dsc inc | cpp/algorithm/dsc set_intersection}}
+
{{dsc inc | cpp/algorithm/ranges/dsc set_intersection}}
+
{{dsc inc | cpp/algorithm/dsc set_symmetric_difference}}
+
{{dsc inc | cpp/algorithm/ranges/dsc set_symmetric_difference}}
+
{{dsc inc | cpp/algorithm/dsc set_union}}
+
{{dsc inc | cpp/algorithm/ranges/dsc set_union}}
+
  
{{dsc h2 | Heap operations}}
+
A heap can be created by {{lc|std::make_heap}}{{rev inl|since=c++20| and {{lc|ranges::make_heap}}}}.
{{dsc header | algorithm}}
+
{{dsc inc | cpp/algorithm/dsc is_heap}}
+
{{dsc inc | cpp/algorithm/ranges/dsc is_heap}}
+
{{dsc inc | cpp/algorithm/dsc is_heap_until}}
+
{{dsc inc | cpp/algorithm/ranges/dsc is_heap_until}}
+
{{dsc inc | cpp/algorithm/dsc make_heap}}
+
{{dsc inc | cpp/algorithm/ranges/dsc make_heap}}
+
{{dsc inc | cpp/algorithm/dsc push_heap}}
+
{{dsc inc | cpp/algorithm/ranges/dsc push_heap}}
+
{{dsc inc | cpp/algorithm/dsc pop_heap}}
+
{{dsc inc | cpp/algorithm/ranges/dsc pop_heap}}
+
{{dsc inc | cpp/algorithm/dsc sort_heap}}
+
{{dsc inc | cpp/algorithm/ranges/dsc sort_heap}}
+
  
{{dsc h2 | Minimum/maximum operations}}
+
For more properties of heap, see {{enwiki|Binary heap|max heap}}.
{{dsc header | algorithm}}
+
{{dsc inc | cpp/algorithm/dsc max}}
+
{{dsc inc | cpp/algorithm/ranges/dsc max}}
+
{{dsc inc | cpp/algorithm/dsc max_element}}
+
{{dsc inc | cpp/algorithm/ranges/dsc max_element}}
+
{{dsc inc | cpp/algorithm/dsc min}}
+
{{dsc inc | cpp/algorithm/ranges/dsc min}}
+
{{dsc inc | cpp/algorithm/dsc min_element}}
+
{{dsc inc | cpp/algorithm/ranges/dsc min_element}}
+
{{dsc inc | cpp/algorithm/dsc minmax}}
+
{{dsc inc | cpp/algorithm/ranges/dsc minmax}}
+
{{dsc inc | cpp/algorithm/dsc minmax_element}}
+
{{dsc inc | cpp/algorithm/ranges/dsc minmax_element}}
+
{{dsc inc | cpp/algorithm/dsc clamp}}
+
{{dsc inc | cpp/algorithm/ranges/dsc clamp}}
+
  
{{dsc h2 | Comparison operations}}
 
{{dsc header | algorithm}}
 
{{dsc inc | cpp/algorithm/dsc equal}}
 
{{dsc inc | cpp/algorithm/ranges/dsc equal}}
 
{{dsc inc | cpp/algorithm/dsc lexicographical_compare}}
 
{{dsc inc | cpp/algorithm/ranges/dsc lexicographical_compare}}
 
{{dsc inc | cpp/algorithm/dsc lexicographical_compare_three_way}}
 
  
{{dsc h2 | Permutation operations}}
+
{{dsc begin}}
{{dsc header | algorithm}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/algorithm/dsc is_permutation}}
+
{{dsc inc|cpp/algorithm/dsc push_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc is_permutation}}
+
{{dsc inc|cpp/algorithm/ranges/dsc push_heap}}
{{dsc inc | cpp/algorithm/dsc next_permutation}}
+
{{dsc inc|cpp/algorithm/dsc pop_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc next_permutation}}
+
{{dsc inc|cpp/algorithm/ranges/dsc pop_heap}}
{{dsc inc | cpp/algorithm/dsc prev_permutation}}
+
{{dsc inc|cpp/algorithm/dsc make_heap}}
{{dsc inc | cpp/algorithm/ranges/dsc prev_permutation}}
+
{{dsc inc|cpp/algorithm/ranges/dsc make_heap}}
 +
{{dsc inc|cpp/algorithm/dsc sort_heap}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc sort_heap}}
 +
{{dsc inc|cpp/algorithm/dsc is_heap}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc is_heap}}
 +
{{dsc inc|cpp/algorithm/dsc is_heap_until}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc is_heap_until}}
 +
{{dsc end}}
  
{{dsc h2 | Numeric operations}}
+
====Minimum/maximum operations====
{{dsc header | numeric}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc iota}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/algorithm/ranges/dsc iota}}
+
{{dsc inc|cpp/algorithm/dsc max}}
{{dsc inc | cpp/algorithm/dsc accumulate}}
+
{{dsc inc|cpp/algorithm/ranges/dsc max}}
{{dsc inc | cpp/algorithm/dsc inner_product}}
+
{{dsc inc|cpp/algorithm/dsc max_element}}
{{dsc inc | cpp/algorithm/dsc adjacent_difference}}
+
{{dsc inc|cpp/algorithm/ranges/dsc max_element}}
{{dsc inc | cpp/algorithm/dsc partial_sum}}
+
{{dsc inc|cpp/algorithm/dsc min}}
{{dsc inc | cpp/algorithm/dsc reduce}}
+
{{dsc inc|cpp/algorithm/ranges/dsc min}}
{{dsc inc | cpp/algorithm/dsc exclusive_scan}}
+
{{dsc inc|cpp/algorithm/dsc min_element}}
{{dsc inc | cpp/algorithm/dsc inclusive_scan}}
+
{{dsc inc|cpp/algorithm/ranges/dsc min_element}}
{{dsc inc | cpp/algorithm/dsc transform_reduce}}
+
{{dsc inc|cpp/algorithm/dsc minmax}}
{{dsc inc | cpp/algorithm/dsc transform_exclusive_scan}}
+
{{dsc inc|cpp/algorithm/ranges/dsc minmax}}
{{dsc inc | cpp/algorithm/dsc transform_inclusive_scan}}
+
{{dsc inc|cpp/algorithm/dsc minmax_element}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc minmax_element}}
 +
{{dsc inc|cpp/algorithm/dsc clamp}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc clamp}}
 +
{{dsc end}}
  
{{dsc h2 | Operations on uninitialized memory}}
+
====Lexicographical comparison operations====
 +
{{dsc begin}}
 +
{{dsc header|algorithm}}
 +
{{dsc inc|cpp/algorithm/dsc lexicographical_compare}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc lexicographical_compare}}
 +
{{dsc inc|cpp/algorithm/dsc lexicographical_compare_three_way}}
 +
{{dsc end}}
  
 +
====Permutation operations====
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | memory}}
+
{{dsc header|algorithm}}
{{dsc inc | cpp/memory/dsc uninitialized_copy}}
+
{{dsc inc|cpp/algorithm/dsc next_permutation}}
{{dsc inc | cpp/memory/ranges/dsc uninitialized_copy}}
+
{{dsc inc|cpp/algorithm/ranges/dsc next_permutation}}
{{dsc inc | cpp/memory/dsc uninitialized_copy_n}}
+
{{dsc inc|cpp/algorithm/dsc prev_permutation}}
{{dsc inc | cpp/memory/ranges/dsc uninitialized_copy_n}}
+
{{dsc inc|cpp/algorithm/ranges/dsc prev_permutation}}
{{dsc inc | cpp/memory/dsc uninitialized_fill}}
+
{{dsc inc|cpp/algorithm/dsc is_permutation}}
{{dsc inc | cpp/memory/ranges/dsc uninitialized_fill}}
+
{{dsc inc|cpp/algorithm/ranges/dsc is_permutation}}
{{dsc inc | cpp/memory/dsc uninitialized_fill_n}}
+
{{dsc end}}
{{dsc inc | cpp/memory/ranges/dsc uninitialized_fill_n}}
+
{{dsc inc | cpp/memory/dsc uninitialized_move}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_move}}
+
{{dsc inc | cpp/memory/dsc uninitialized_move_n}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_move_n}}
+
{{dsc inc | cpp/memory/dsc uninitialized_default_construct}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_default_construct}}
+
{{dsc inc | cpp/memory/dsc uninitialized_default_construct_n}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_default_construct_n}}
+
{{dsc inc | cpp/memory/dsc uninitialized_value_construct}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_value_construct}}
+
{{dsc inc | cpp/memory/dsc uninitialized_value_construct_n}}
+
{{dsc inc | cpp/memory/ranges/dsc uninitialized_value_construct_n}}
+
{{dsc inc | cpp/memory/dsc destroy}}
+
{{dsc inc | cpp/memory/ranges/dsc destroy}}
+
{{dsc inc | cpp/memory/dsc destroy_n}}
+
{{dsc inc | cpp/memory/ranges/dsc destroy_n}}
+
{{dsc inc | cpp/memory/dsc destroy_at}}
+
{{dsc inc | cpp/memory/ranges/dsc destroy_at}}
+
{{dsc inc | cpp/memory/dsc construct_at}}
+
{{dsc inc | cpp/memory/ranges/dsc construct_at}}
+
  
{{dsc h2 | C library}}
+
===Numeric operations===
{{dsc header | cstdlib}}
+
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc qsort}}
+
{{dsc header|numeric}}
{{dsc inc | cpp/algorithm/dsc bsearch}}
+
{{dsc inc|cpp/algorithm/dsc iota}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc iota}}
 +
{{dsc inc|cpp/algorithm/dsc accumulate}}
 +
{{dsc inc|cpp/algorithm/dsc inner_product}}
 +
{{dsc inc|cpp/algorithm/dsc adjacent_difference}}
 +
{{dsc inc|cpp/algorithm/dsc partial_sum}}
 +
{{dsc inc|cpp/algorithm/dsc reduce}}
 +
{{dsc inc|cpp/algorithm/dsc exclusive_scan}}
 +
{{dsc inc|cpp/algorithm/dsc inclusive_scan}}
 +
{{dsc inc|cpp/algorithm/dsc transform_reduce}}
 +
{{dsc inc|cpp/algorithm/dsc transform_exclusive_scan}}
 +
{{dsc inc|cpp/algorithm/dsc transform_inclusive_scan}}
 
{{dsc end}}
 
{{dsc end}}
 +
 +
===Operations on uninitialized memory===
 +
{{dsc begin}}
 +
{{dsc header|memory}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_copy}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_copy}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_copy_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_copy_n}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_fill}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_fill}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_fill_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_fill_n}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_move}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_move}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_move_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_move_n}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_default_construct}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_default_construct}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_default_construct_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_default_construct_n}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_value_construct}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_value_construct}}
 +
{{dsc inc|cpp/memory/dsc uninitialized_value_construct_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc uninitialized_value_construct_n}}
 +
{{dsc inc|cpp/memory/dsc destroy}}
 +
{{dsc inc|cpp/memory/ranges/dsc destroy}}
 +
{{dsc inc|cpp/memory/dsc destroy_n}}
 +
{{dsc inc|cpp/memory/ranges/dsc destroy_n}}
 +
{{dsc inc|cpp/memory/dsc destroy_at}}
 +
{{dsc inc|cpp/memory/ranges/dsc destroy_at}}
 +
{{dsc inc|cpp/memory/dsc construct_at}}
 +
{{dsc inc|cpp/memory/ranges/dsc construct_at}}
 +
{{dsc end}}
 +
 +
===Random number generation===
 +
{{dsc begin}}
 +
{{dsc header|random}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc generate_random}}
 +
{{dsc end}}
 +
 +
===Notes===
 +
{{ftm begin|sort=yes}}
 +
{{ftm|std=C++23|value=202207L|__cpp_lib_algorithm_iterator_requirements|Ranges iterators as inputs to non-Ranges algorithms}}
 +
{{ftm|std=C++17|value=201603L|__cpp_lib_clamp|{{lc|std::clamp}}}}
 +
{{ftm|std=C++20|value=201806L|__cpp_lib_constexpr_algorithms|rowspan="2"|Constexpr for algorithms}}
 +
{{ftm|std=C++26|value=202306L|-|Constexpr stable sorting}}
 +
{{ftm|std=C++26|value=202403L|__cpp_lib_algorithm_default_value_type|[[cpp/language/list initialization|List-initialization]] for algorithms}}
 +
{{ftm|std=C++26|value=202311L|__cpp_lib_freestanding_algorithm|Freestanding facilities in {{header|algorithm}}}}
 +
{{ftm|std=C++14|value=201304L|__cpp_lib_robust_nonmodifying_seq_ops|Making non-modifying sequence operations more robust (two-range overloads for {{lc|std::mismatch}}, {{lc|std::equal}} and {{lc|std::is_permutation)}}}}
 +
{{ftm|std=C++17|value=201603L|__cpp_lib_sample|{{lc|std::sample}}}}
 +
{{ftm|std=C++20|value=201806L|__cpp_lib_shift|{{lc|std::shift_left}} and {{lc|std::shift_right}}}}
 +
{{ftm end}}
 +
 +
===C library===
 +
{{dsc begin}}
 +
{{dsc header|cstdlib}}
 +
{{dsc inc|cpp/algorithm/dsc qsort}}
 +
{{dsc inc|cpp/algorithm/dsc bsearch}}
 +
{{dsc end}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=193|std=C++98|before=heap required {{c|*first}} to be the largest element|after=there can be elements<br>equal to {{c|*first}}}}
 +
{{dr list item|wg=lwg|dr=2150|std=C++98|before=the definition of a sorted sequence was incorrect|after=corrected}}
 +
{{dr list item|wg=lwg|dr=2166|std=C++98|before=the heap requirement did not match the<br>definition of {{enwiki|Binary heap|max heap}} closely enough|after=requirement improved}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see c | c/algorithm | Algorithms | nomono=true}}
+
{{dsc see c|c/algorithm|Algorithms|nomono=true}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|ar|de|es|fr|it|ja|pt|ru|tr|zh}}
 
{{langlinks|ar|de|es|fr|it|ja|pt|ru|tr|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
 

The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements. Note that a range is defined as [firstlast) where last refers to the element past the last element to inspect or modify.

Contents

Constrained algorithms

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.

std::vector<int> v {7, 1, 4, 0, -1};
std::ranges::sort(v); // constrained algorithm
(since C++20)


Execution policies

Most algorithms have overloads that accept execution policies. The standard library algorithms support several execution policies, and the library provides corresponding execution policy types and objects. Users may select an execution policy statically by invoking a parallel algorithm with an execution policy object of the corresponding type.

Standard library implementations (but not the users) may define additional execution policies as an extension. The semantics of parallel algorithms invoked with an execution policy object of implementation-defined type is implementation-defined.

Parallel version of algorithms (except for std::for_each and std::for_each_n) are allowed to make arbitrary copies of elements from ranges, as long as both std::is_trivially_copy_constructible_v<T> and std::is_trivially_destructible_v<T> are true, where T is the type of elements.

Defined in header <execution>
Defined in namespace std::execution
execution policy types
(class) [edit]
(C++17)(C++17)(C++17)(C++20)
global execution policy objects
(constant) [edit]
Defined in namespace std
test whether a class represents an execution policy
(class template) [edit]
Feature-test macro Value Std Feature
__cpp_lib_parallel_algorithm 201603L (C++17) Parallel algorithms
__cpp_lib_execution 201603L (C++17) Execution policies
201902L (C++20) std::execution::unsequenced_policy
(since C++17)

[edit] Non-modifying sequence operations

[edit] Batch operations

Defined in header <algorithm>
applies a function to a range of elements
(function template) [edit]
applies a function to a range of elements
(niebloid)[edit]
applies a function object to the first N elements of a sequence
(function template) [edit]
applies a function object to the first N elements of a sequence
(niebloid)[edit]

[edit] Search operations

Defined in header <algorithm>
(C++11)(C++11)(C++11)
checks if a predicate is true for all, any or none of the elements in a range
(function template) [edit]
checks if a predicate is true for all, any or none of the elements in a range
(niebloid)[edit]
checks if the range contains the given element or subrange
(niebloid)[edit]
finds the first element satisfying specific criteria
(function template) [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
(function template) [edit]
finds the last sequence of elements in a certain range
(niebloid)[edit]
searches for any one of a set of elements
(function template) [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)
(function template) [edit]
finds the first two adjacent items that are equal (or satisfy a given predicate)
(niebloid)[edit]
returns the number of elements satisfying specific criteria
(function template) [edit]
returns the number of elements satisfying specific criteria
(niebloid)[edit]
finds the first position where two ranges differ
(function template) [edit]
finds the first position where two ranges differ
(niebloid)[edit]
determines if two sets of elements are the same
(function template) [edit]
determines if two sets of elements are the same
(niebloid)[edit]
searches for the first occurrence of a range of elements
(function template) [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
(function template) [edit]
searches for the first occurrence of a number consecutive copies of an element in a range
(niebloid)[edit]
checks whether a range starts with another range
(niebloid)[edit]
checks whether a range ends with another range
(niebloid)[edit]

[edit] Fold operations

Defined in header <algorithm>
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] Modifying sequence operations

[edit] Copy operations

Defined in header <algorithm>
copies a range of elements to a new location
(function template) [edit]
copies a range of elements to a new location
(niebloid)[edit]
(C++11)
copies a number of elements to a new location
(function template) [edit]
copies a number of elements to a new location
(niebloid)[edit]
copies a range of elements in backwards order
(function template) [edit]
copies a range of elements in backwards order
(niebloid)[edit]
(C++11)
moves a range of elements to a new location
(function template) [edit]
moves a range of elements to a new location
(niebloid)[edit]
moves a range of elements to a new location in backwards order
(function template) [edit]
moves a range of elements to a new location in backwards order
(niebloid)[edit]

[edit] Swap operations

Defined in header <algorithm>   (until C++11)
Defined in header <utility>     (since C++11)
Defined in header <string_view>
swaps the values of two objects
(function template) [edit]
Defined in header <algorithm>
swaps two ranges of elements
(function template) [edit]
swaps two ranges of elements
(niebloid)[edit]
swaps the elements pointed to by two iterators
(function template) [edit]

[edit] Transformation operations

Defined in header <algorithm>
applies a function to a range of elements, storing results in a destination range
(function template) [edit]
applies a function to a range of elements
(niebloid)[edit]
replaces all values satisfying specific criteria with another value
(function template) [edit]
replaces all values satisfying specific criteria with another value
(niebloid)[edit]
copies a range, replacing elements satisfying specific criteria with another value
(function template) [edit]
copies a range, replacing elements satisfying specific criteria with another value
(niebloid)[edit]

[edit] Generation operations

Defined in header <algorithm>
copy-assigns the given value to every element in a range
(function template) [edit]
assigns a range of elements a certain value
(niebloid)[edit]
copy-assigns the given value to N elements in a range
(function template) [edit]
assigns a value to a number of elements
(niebloid)[edit]
assigns the results of successive function calls to every element in a range
(function template) [edit]
saves the result of a function in a range
(niebloid)[edit]
assigns the results of successive function calls to N elements in a range
(function template) [edit]
saves the result of N applications of a function
(niebloid)[edit]

[edit] Removing operations

Defined in header <algorithm>
removes elements satisfying specific criteria
(function template) [edit]
removes elements satisfying specific criteria
(niebloid)[edit]
copies a range of elements omitting those that satisfy specific criteria
(function template) [edit]
copies a range of elements omitting those that satisfy specific criteria
(niebloid)[edit]
removes consecutive duplicate elements in a range
(function template) [edit]
removes consecutive duplicate elements in a range
(niebloid)[edit]
creates a copy of some range of elements that contains no consecutive duplicates
(function template) [edit]
creates a copy of some range of elements that contains no consecutive duplicates
(niebloid)[edit]

[edit] Order-changing operations

Defined in header <algorithm>
reverses the order of elements in a range
(function template) [edit]
reverses the order of elements in a range
(niebloid)[edit]
creates a copy of a range that is reversed
(function template) [edit]
creates a copy of a range that is reversed
(niebloid)[edit]
rotates the order of elements in a range
(function template) [edit]
rotates the order of elements in a range
(niebloid)[edit]
copies and rotate a range of elements
(function template) [edit]
copies and rotate a range of elements
(niebloid)[edit]
shifts elements in a range
(function template) [edit]
(until C++17)(C++11)
randomly re-orders elements in a range
(function template) [edit]
randomly re-orders elements in a range
(niebloid)[edit]
shifts elements in a range
(niebloid)[edit]

[edit] Sampling operations

Defined in header <algorithm>
(C++17)
selects N random elements from a sequence
(function template) [edit]
selects N random elements from a sequence
(niebloid)[edit]

[edit] Sorting and related operations

[edit] Requirements

Some algorithms require the sequence represented by the arguments to be “sorted” or “partitioned”. The behavior is undefined if the requirement is not met.

A sequence is sorted with respect to a comparator comp if for every iterator iter pointing to the sequence and every non-negative integer n such that iter + n[1] is a valid iterator pointing to an element of the sequence, comp(*(iter + n), *iter) == false[1].

(until C++20)

A sequence is sorted with respect to comp and proj for a comparator comp and projection proj if for every iterator iter pointing to the sequence and every non-negative integer n such that iter + n[1] is a valid iterator pointing to an element of the sequence, bool(std::invoke(comp, std::invoke(proj, *(iter + n)),
                       std::invoke(proj, *iter)))
[1] is false.

A sequence is sorted with respect to a comparator comp if the sequence is sorted with respect to comp and std::identity{} (the identity projection).

(since C++20)

A sequence [startfinish) is partitioned with respect to an expression f(e) if there exists an integer n such that for all i in [0std::distance(start, finish)), f(*(start + i))[1] is true if and only if i < n.

  1. 1.0 1.1 1.2 1.3 1.4 iter + n simply means “the result of iter being incremented n times”, regardless of whether iter is a random access iterator.

[edit] Partitioning operations

Defined in header <algorithm>
determines if the range is partitioned by the given predicate
(function template) [edit]
determines if the range is partitioned by the given predicate
(niebloid)[edit]
divides a range of elements into two groups
(function template) [edit]
divides a range of elements into two groups
(niebloid)[edit]
copies a range dividing the elements into two groups
(function template) [edit]
copies a range dividing the elements into two groups
(niebloid)[edit]
divides elements into two groups while preserving their relative order
(function template) [edit]
divides elements into two groups while preserving their relative order
(niebloid)[edit]
locates the partition point of a partitioned range
(function template) [edit]
locates the partition point of a partitioned range
(niebloid)[edit]

[edit] Sorting operations

Defined in header <algorithm>
sorts a range into ascending order
(function template) [edit]
sorts a range into ascending order
(niebloid)[edit]
sorts a range of elements while preserving order between equal elements
(function template) [edit]
sorts a range of elements while preserving order between equal elements
(niebloid)[edit]
sorts the first N elements of a range
(function template) [edit]
sorts the first N elements of a range
(niebloid)[edit]
copies and partially sorts a range of elements
(function template) [edit]
copies and partially sorts a range of elements
(niebloid)[edit]
(C++11)
checks whether a range is sorted into ascending order
(function template) [edit]
checks whether a range is sorted into ascending order
(niebloid)[edit]
finds the largest sorted subrange
(function template) [edit]
finds the largest sorted subrange
(niebloid)[edit]
partially sorts the given range making sure that it is partitioned by the given element
(function template) [edit]
partially sorts the given range making sure that it is partitioned by the given element
(niebloid)[edit]

[edit] Binary search operations (on partitioned ranges)

Defined in header <algorithm>
returns an iterator to the first element not less than the given value
(function template) [edit]
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
(function template) [edit]
returns an iterator to the first element greater than a certain value
(niebloid)[edit]
returns range of elements matching a specific key
(function template) [edit]
returns range of elements matching a specific key
(niebloid)[edit]
determines if an element exists in a partially-ordered range
(function template) [edit]
determines if an element exists in a partially-ordered range
(niebloid)[edit]

[edit] Set operations (on sorted ranges)

Defined in header <algorithm>
returns true if one sequence is a subsequence of another
(function template) [edit]
returns true if one sequence is a subsequence of another
(niebloid)[edit]
computes the union of two sets
(function template) [edit]
computes the union of two sets
(niebloid)[edit]
computes the intersection of two sets
(function template) [edit]
computes the intersection of two sets
(niebloid)[edit]
computes the difference between two sets
(function template) [edit]
computes the difference between two sets
(niebloid)[edit]
computes the symmetric difference between two sets
(function template) [edit]
computes the symmetric difference between two sets
(niebloid)[edit]

[edit] Merge operations (on sorted ranges)

Defined in header <algorithm>
merges two sorted ranges
(function template) [edit]
merges two sorted ranges
(niebloid)[edit]
merges two ordered ranges in-place
(function template) [edit]
merges two ordered ranges in-place
(niebloid)[edit]

[edit] Heap operations

A random access range [firstlast) is a heap with respect to a comparator comp if bool(comp(first[(i - 1) / 2], first[i])) is false for all integer i in (0last - first).

(until C++20)

A random access range [firstlast) is a heap with respect to comp and proj for a comparator comp and projection proj if bool(std::invoke(comp, std::invoke(proj, first[(i - 1) / 2]),
                       std::invoke(proj, first[i]))
is false for all integer i in (0last - first).

A random access range [firstlast) is a heap with respect to a comparator comp if the range is a heap with respect to comp and std::identity{} (the identity projection).

(since C++20)

A heap can be created by std::make_heap and ranges::make_heap(since C++20).

For more properties of heap, see max heap.


Defined in header <algorithm>
adds an element to a max heap
(function template) [edit]
adds an element to a max heap
(niebloid)[edit]
removes the largest element from a max heap
(function template) [edit]
removes the largest element from a max heap
(niebloid)[edit]
creates a max heap out of a range of elements
(function template) [edit]
creates a max heap out of a range of elements
(niebloid)[edit]
turns a max heap into a range of elements sorted in ascending order
(function template) [edit]
turns a max heap into a range of elements sorted in ascending order
(niebloid)[edit]
(C++11)
checks if the given range is a max heap
(function template) [edit]
checks if the given range is a max heap
(niebloid)[edit]
finds the largest subrange that is a max heap
(function template) [edit]
finds the largest subrange that is a max heap
(niebloid)[edit]

[edit] Minimum/maximum operations

Defined in header <algorithm>
returns the greater of the given values
(function template) [edit]
returns the greater of the given values
(niebloid)[edit]
returns the largest element in a range
(function template) [edit]
returns the largest element in a range
(niebloid)[edit]
returns the smaller of the given values
(function template) [edit]
returns the smaller of the given values
(niebloid)[edit]
returns the smallest element in a range
(function template) [edit]
returns the smallest element in a range
(niebloid)[edit]
(C++11)
returns the smaller and larger of two elements
(function template) [edit]
returns the smaller and larger of two elements
(niebloid)[edit]
returns the smallest and the largest elements in a range
(function template) [edit]
returns the smallest and the largest elements in a range
(niebloid)[edit]
(C++17)
clamps a value between a pair of boundary values
(function template) [edit]
clamps a value between a pair of boundary values
(niebloid)[edit]

[edit] Lexicographical comparison operations

Defined in header <algorithm>
returns true if one range is lexicographically less than another
(function template) [edit]
returns true if one range is lexicographically less than another
(niebloid)[edit]
compares two ranges using three-way comparison
(function template) [edit]

[edit] Permutation operations

Defined in header <algorithm>
generates the next greater lexicographic permutation of a range of elements
(function template) [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
(function template) [edit]
generates the next smaller lexicographic permutation of a range of elements
(niebloid)[edit]
determines if a sequence is a permutation of another sequence
(function template) [edit]
determines if a sequence is a permutation of another sequence
(niebloid)[edit]

[edit] Numeric operations

Defined in header <numeric>
(C++11)
fills a range with successive increments of the starting value
(function template) [edit]
fills a range with successive increments of the starting value
(niebloid)[edit]
sums up or folds a range of elements
(function template) [edit]
computes the inner product of two ranges of elements
(function template) [edit]
computes the differences between adjacent elements in a range
(function template) [edit]
computes the partial sum of a range of elements
(function template) [edit]
(C++17)
similar to std::accumulate, except out of order
(function template) [edit]
similar to std::partial_sum, excludes the ith input element from the ith sum
(function template) [edit]
similar to std::partial_sum, includes the ith input element in the ith sum
(function template) [edit]
applies an invocable, then reduces out of order
(function template) [edit]
applies an invocable, then calculates exclusive scan
(function template) [edit]
applies an invocable, then calculates inclusive scan
(function template) [edit]

[edit] Operations on uninitialized memory

Defined in header <memory>
copies a range of objects to an uninitialized area of memory
(function template) [edit]
copies a range of objects to an uninitialized area of memory
(niebloid)[edit]
copies a number of objects to an uninitialized area of memory
(function template) [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
(function template) [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
(function template) [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
(function template) [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
(function template) [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
(function template) [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 a count
(function template) [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
(function template) [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
(function template) [edit]
constructs objects by value-initialization in an uninitialized area of memory, defined by a start and a count
(niebloid)[edit]
(C++17)
destroys a range of objects
(function template) [edit]
destroys a range of objects
(niebloid)[edit]
(C++17)
destroys a number of objects in a range
(function template) [edit]
destroys a number of objects in a range
(niebloid)[edit]
destroys an object at a given address
(function template) [edit]
destroys an object at a given address
(niebloid)[edit]
creates an object at a given address
(function template) [edit]
creates an object at a given address
(niebloid)[edit]

[edit] Random number generation

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

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_algorithm_iterator_requirements 202207L (C++23) Ranges iterators as inputs to non-Ranges algorithms
__cpp_lib_clamp 201603L (C++17) std::clamp
__cpp_lib_constexpr_algorithms 201806L (C++20) Constexpr for algorithms
202306L (C++26) Constexpr stable sorting
__cpp_lib_algorithm_default_value_type 202403L (C++26) List-initialization for algorithms
__cpp_lib_freestanding_algorithm 202311L (C++26) Freestanding facilities in <algorithm>
__cpp_lib_robust_nonmodifying_seq_ops 201304L (C++14) Making non-modifying sequence operations more robust (two-range overloads for std::mismatch, std::equal and std::is_permutation)
__cpp_lib_sample 201603L (C++17) std::sample
__cpp_lib_shift 201806L (C++20) std::shift_left and std::shift_right

[edit] C library

Defined in header <cstdlib>
sorts a range of elements with unspecified type
(function) [edit]
searches an array for an element of unspecified type
(function) [edit]

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 193 C++98 heap required *first to be the largest element there can be elements
equal to *first
LWG 2150 C++98 the definition of a sorted sequence was incorrect corrected
LWG 2166 C++98 the heap requirement did not match the
definition of max heap closely enough
requirement improved

[edit] See also

C documentation for Algorithms