Algorithm (C++) (original) (raw)

From Wikipedia, the free encyclopedia

C++ Standard Library header providing algorithm implementations

C++ Standard Library
Input/output Strings algorithm functional
Containers
Sequence containers Associative containers Unordered associative containers
C standard library
Data types Character classification Strings Mathematics File input/output Date/time Localization Memory allocation Process control Signals Alternative tokens Miscellaneous headers:<assert.h> <errno.h> <setjmp.h> <stdarg.h>
vte

In the C++ Standard Library, the algorithms library provides various functions that perform algorithmic operations on containers and other sequences, represented by Iterators.[1]

The C++ standard provides some standard algorithms collected in the <algorithm> standard header.[2] A handful of algorithms are also in the <numeric> header. All algorithms are in the std namespace.

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

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.

C++20 adds versions of the algorithms defined in the <algorithm> header which operate on ranges rather than pairs of iterators.

The ranges versions of algorithm functions are scoped within the 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

[edit]

Predicate checking algorithms

[edit]

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

Comparison algorithms

[edit]

Compares two ranges for some property

Searching algorithms

[edit]

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

Binary search algorithms

[edit]

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

Maximum/Minimum search algorithms

[edit]

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

Property checking algorithms

[edit]

Checks if an entire range satisfies some property

Modifying sequence operations

[edit]

Transfers the elements from one range into another

Partitioning algorithms

[edit]

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

Sorts or partially sorts a range in-place

Populating algorithms

[edit]

Populates a given range without reading the values contained within

Transforming algorithms

[edit]

Transforms each element of a given range in-place

Reordering algorithms

[edit]

Changes the order of elements within a range in-place

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

  1. ^ ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ ยง25 Algorithms library [lib.algorithms] para. 1
  2. ^ Stroustrup, Bjarne (2009). Programming : principles and practice using C++. Upper Saddle River, NJ: Addison-Wesley. p. 729. ISBN 9780321543721. Retrieved 22 March 2012. The standard library algorithms are found in <algorithm>.