algorithm (C++)

{{Short description|C++ Standard Library header providing algorithm implementations}}

{{C++ Standard library}}

In the C++ Standard Library, the algorithms library provides various functions that perform algorithmic operations on containers and other sequences, represented by Iterators.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §25 Algorithms library [lib.algorithms] para. 1

The C++ standard provides some standard algorithms collected in the standard header.{{cite book|last=Stroustrup|first=Bjarne|title=Programming : principles and practice using C++|year=2009|publisher=Addison-Wesley|location=Upper Saddle River, NJ|isbn=9780321543721|url=http://www.pearsonhighered.com/educator/product/Programming-Principles-and-Practice-Using-C/9780321543721.page|access-date=22 March 2012|page=729|quote=The standard library algorithms are found in .}} A handful of algorithms are also in the header. All algorithms are in the {{Cpp|std}} namespace.

Execution policies

C++17 provides the ability for many algorithms to optionally take an execution policy, which may allow implementations to execute the algorithm in parallel (i.e. by using threads or SIMD instructions).

There are four different policy types, each indicating different semantics about the order in which element accesses are allowed to be observed relative to each other

  • {{Cpp|sequenced_policy}}, which indicates that the execution of the algorithm must happen on the thread which invokes the function, and the order of element accesses should execute in sequence. It is equivalent to calling the function without an execution policy
  • {{Cpp|parallel_policy}}, which indicates that the execution of the algorithm may happen across multiple threads, however within each thread the order of element accesses are made in sequence (i.e. element accesses may not be done concurrently)
  • {{Cpp|parallel_unsequenced_policy}}, which indicates that the execution of the algorithm may happen across multiple threads, and element accesses do not have to be performed in order within the same thread
  • {{Cpp|unsequenced_policy}}, which indicates that the execution of the algorithm must happen on the thread which invokes the function, however the order of element accesses may be performed out of sequence

It is up to the user to ensure that the operations performed by the function are thread safe when using policies which may execute across different threads.

Ranges

C++20 adds versions of the algorithms defined in the {{Cpp|}} header which operate on ranges rather than pairs of iterators.

The ranges versions of algorithm functions are scoped within the {{Cpp|ranges}} namespace. They extend the functionality of the basic algorithms by allowing iterator-sentinel pairs to be used instead of requiring that both iterators be of the same type and also allowing interoperability with the objects provided by the ranges header without requiring the user to manually extract the iterators.

Non-modifying sequence operations

= Predicate checking algorithms =

Checks if a given predicate evaluates to true for some amount of objects in the range, or returns the amount of objects that do

  • {{Cpp|all_of}}
  • {{Cpp|any_of}}
  • {{Cpp|none_of}}
  • {{Cpp|count}}
  • {{Cpp|count_if}}
  • {{Cpp|contains}}

= Comparison algorithms =

Compares two ranges for some property

  • {{Cpp|mismatch}}
  • {{Cpp|equal}}
  • {{Cpp|lexicographical_compare}}
  • {{Cpp|contains_subrange}}
  • {{Cpp|starts_with}}
  • {{Cpp|ends_with}}
  • {{Cpp|is_permutation}}

= Searching algorithms =

Finds the first or last position in a range where the subsequent elements satisfy some predicate

  • {{Cpp|find}}
  • {{Cpp|find_if}}
  • {{Cpp|find_if_not}}
  • {{Cpp|find_last}}
  • {{Cpp|find_last_if}}
  • {{Cpp|find_last_if_not}}
  • {{Cpp|find_end}}
  • {{Cpp|find_first_of}}
  • {{Cpp|adjacent_find}}
  • {{Cpp|search}}
  • {{Cpp|search_n}}
  • {{Cpp|partition_point}}

= Binary search algorithms =

Provides Binary search operations on ranges. It is undefined behaviour to use these on ranges which are not sorted.

  • {{Cpp|binary_search}}
  • {{Cpp|upper_bound}}
  • {{Cpp|lower_bound}}
  • {{Cpp|equal_range}}

= Maximum/Minimum search algorithms =

Finds the maximum or minimum element in a range, as defined by some comparison predicate

  • {{Cpp|max_element}}
  • {{Cpp|min_element}}
  • {{Cpp|minmax_element}}

= Property checking algorithms =

Checks if an entire range satisfies some property

  • {{Cpp|is_partitioned}}
  • {{Cpp|is_sorted}}
  • {{Cpp|is_heap}}

Modifying sequence operations

= Copying algorithms =

Transfers the elements from one range into another

  • {{Cpp|copy}}
  • {{Cpp|copy_if}}
  • {{Cpp|copy_backward}}
  • {{Cpp|move}}
  • {{Cpp|move_backward}}
  • {{Cpp|reverse_copy}}
  • {{Cpp|rotate_copy}}
  • {{Cpp|unique_copy}}
  • {{Cpp|sample}}

= Partitioning algorithms =

Moves the elements of a range in-place so the range is partitioned with respect to some property

  • {{Cpp|unique}}
  • {{Cpp|remove}}
  • {{Cpp|remove_if}}
  • {{Cpp|partition}}
  • {{Cpp|partition_copy}}
  • {{Cpp|stable_partition}}

= Sorting algorithms =

Sorts or partially sorts a range in-place

  • {{Cpp|sort}}
  • {{Cpp|partial sort}}
  • {{Cpp|stable_sort}}
  • {{Cpp|nth_element}}

= Populating algorithms =

Populates a given range without reading the values contained within

  • {{Cpp|fill}}
  • {{Cpp|generate}}
  • {{Cpp|iota}}

= Transforming algorithms =

Transforms each element of a given range in-place

  • {{Cpp|for_each}}
  • {{Cpp|transform}}
  • {{Cpp|replace}}
  • {{Cpp|replace_if}}
  • {{Cpp|clamp}}

= Reordering algorithms =

Changes the order of elements within a range in-place

  • {{Cpp|shuffle}}
  • {{Cpp|shift_left}}
  • {{Cpp|shift_right}}
  • {{Cpp|reverse}}
  • {{Cpp|rotate}}

= Heap algorithms =

Provides algorithms to create, insert, and remove elements from a max heap

References

{{reflist}}