Algorithm Intuition
This page exists to catalog and explore the C++ STL algorithms, and to name and justify missed algorithms.
The name "algorithm intuition" comes from Kate Gregory by way of Conor Hoekstra. It refers to an ability to look at a problem and break it down into a series of high-level algorithmic steps, rather than as a low-level series of discrete steps, in the same way as skilled programmers are able to look at a set of characteristics and select the correct abstract data structure, without having to think about the discrete nodes or pointers that make it up. Building up an intuition for algorithms is essential to being a great C++ programmer, and makes a huge difference in the clarity, consiceness, and ultimately correctness of one's code.
This webpage is an attempt to take the long list of over a hundred named algorithms and break them down into families, providing an easy high-level view of what they do, expressed in the form of input, output, and abstract operations. If you want to link to a specific row in the table, you can click the # in the upper-left corner of that row's name column.
The source code and data I used to generate this table (not including the source for this page itself) is located here. The PHP code I used to transclude the generated tables into this page is available here.
I do not actually know if a search (and particularly, a search which returns a whole subrange) is strictly speaking a catamorphism, but even if they are not, there are very strong links between several of the searching and folding algorithms, and several of the folds can be implemented in terms of a related search algorithm. I am also uncertain about whether everything I have labelled as an anamorphism truly is one, as explained below∅.
Blue
names indicate algorithms described by Conor. Green
names are from kblib. Occasionally, I may add algorithms specified in C++ standard proposals, which will be indicated in orange
. Red
names in the "Default operations" column indicate non-overridable parameters and a lack of generality. The notes below the tables explain the rest of the terminology and notation I used.
Where kblib provides an alternate definition of a standard library algorithm, usually only the standard algorithm is shown, unless the two differ in some significant way. For minor differences, which affect only one or two columns, two values may be shown, with the kblib version colored green
.
Catamorphisms (folds) and Searches
Name | Input ranges* | Accumulator | Returns⌖ | Operations† | Default operations | Complexity | Order‡ | Compare to |
---|---|---|---|---|---|---|---|---|
2 | Arg | Value | A, bT | plus , multiplies |
Unspc. | Fwd. | transform_ |
|
1s | Arg | Value | A, bT | Unspc. | Fwd. | inner_ |
||
1 / 2 | Arg | Value | acR, uT / bT | plus , multiplies |
O(N) | ∥ | ||
1+Value | Position | equal_to |
O(N) | S/C∥ | ||||
1 | Position | uP | O(N) | S/C∥ | ||||
2 | Position | bP | equal_to |
O(S×N) | S/C∥ | find_if |
||
1 | First | Position | bP | less |
O(N), =(max(N-1, 0)) | Fwd.∥ | ||
1 | First | 2 Positions⌖ | bP | less |
O(N), ≤(max(floor((3/2)*(N−1)), 0)) | Fwd.∥ | ||
1+Value | Position | bP | less |
O(log N) + O(1)↓R; O(N) | B/S | |||
1+Value | Range⌖ | bP | less |
O(log N) + O(1)↓R; O(N) | B/S | |||
2 | Position | bP | equal_to |
≤(S×N) | ∥ | |||
2 | Position | bP | equal_to |
≤(S×(N-S+1)) | ∥ | search |
||
2 | bool |
bP | equal_to |
N > S ? O(S) : O(1)↓R; O(min(S, N)) | Fwd. | |||
2 | bool |
bP | equal_to |
N > S ? O(S) : O(1)↓R; O(min(S, N)) | ||||
2 | bool |
bP, uT, uTΔ | equal_to , identity , identity |
≤(min(S, N)) | ||||
1 | Position | Searcher | Depends on Searcher | |||||
2 | Position⌖ | bP | equal_to |
O(N), O(min(N, M)) | S/C | |||
2 | Position⌖ | bP | equal_to |
O(N), O(min(N, M)) | S/C∥ | find_match |
||
1s | Position | bP | equal_to |
=(min((result-first)+1, (last-first)-1)); O(N)↓∥ | S/C∥ | find_match |
||
1s | Position | bP | less |
O(N) | S/C∥ | find_match |
||
1 + Count + Value | Position | bP | equal_to |
≤(N) | ∥ | adjacent_ |
||
1 | Arg | Value | A | plus |
Unspc.; O(N) | Fwd. | ||
1 | First | Value | R | plus |
O(N) | Fwd. | accumulate |
|
1 | Arg | Value | acR | plus |
O(N) | ∥ | ||
1+Value | 0 |
size_t |
equal_to |
=(N) | Fwd.∥ | accumulate |
||
1 | 0 |
size_t |
uP | =(N) | Fwd.∥ | accumulate |
||
1+Value | bool |
bP | less |
O(log N) + O(1)↓R; O(N) | B/S | |||
1 | bool |
uP | O(N) | Fwd.∥ | ||||
1 | bool |
bP | less |
O(N) | Fwd.∥ | |||
1 | bool |
bP | less |
O(N) | ∥ | |||
2 | bool |
bP | equal_to |
N==M ? O(N2) : O(1)↓R; O(N2) | ||||
2 | bool |
bP | less |
O(N+M), ≤(2×(N+M-1)) | Fwd.∥ | |||
1+Value / 2+Value | Arg | Value | uT / bT | O(N) | S/C | |||
1 / 2 | Value | uT / bT, uP / bP | O(N) | S/C | ||||
1 | true |
bool |
uP | O(N) | S/C∥ | first_result |
||
1 | false |
bool |
uP | O(N) | S/C∥ | first_result |
||
1+Value | false |
bool |
equal_to |
O(N) | S/C | first_result |
||
2 | false |
bool |
contains |
O(S×N) | S/C | first_result |
||
2 | bool |
bP | equal_to |
N==M ? ≤(N) : O(1)↓R; ≤(N); O(N)↓∥ | Fwd.∥ | first_result |
||
2 | bool |
bP | less |
≤(2×min(N, M)) | Fwd.∥ | first_result |
||
2 | ordering | CompareΔ | compare_ |
<(min(N, M)) | Fwd. | first_result |
||
1 | Position | uP | O(log N)↓R; O(N) | B/S | ||||
1 | Position | bP | less |
O(N) | ∥ | |||
Name | Input ranges* | Accumulator | Returns⌖ | Operations† | Default operations | Complexity | Order‡ | Compare to |
Anamorphisms (unfolds/transforms)
Name | Input ranges* | Accumulator | Output ranges | Operations† | Default operations | Complexity | Order‡ | Compare to |
---|---|---|---|---|---|---|---|---|
1 | First, Arg | 1 | aR, uT | O(N) | ∥ | |||
1 | Arg | 1 | aR, uT | O(N) | ∥ | |||
1 | First | 1 | A | plus |
O(N), =(N - 1) | Fwd. | ||
1 | First, Arg | 1 | aR | plus |
O(N) | ∥ | ||
1 | Arg | 1 | aR | plus |
O(N) | ∥ | ||
1s | Quasi | 1 | fD | minus |
O(N), =(N - 1) | Fwd.∥ | adjacent_ |
|
1s | Quasi | 1 | D | O(N), =(N - 1) | Fwd.∥ | adjacent_ |
||
1s | First | 1 | A, D | O(N) | Fwd. | |||
1 / 2 | 1 | uT / bT | =(N) | ∥ | ||||
1 | 1 | =(N) | Fwd.∥ | transform |
||||
1 | 1 | =(N) | Rev. | |||||
1 | 1 | =(N) | Unspc.∥ | transform |
||||
1 | 1 | =(N) | Fwd.∥ | shift_left |
||||
1 | 1 | =(N) | Rev. | shift_ |
||||
1+Value | Arg | 1 | equal_to |
=(N) | Fwd.∥ | transform |
||
1 | Arg | 1 | uP | =(N) | Fwd.∥ | transform |
||
1 | 1 | =(N) | ∥ | transform |
||||
1d | 1 | O(N) | ∥ | |||||
1 | Arg | 1 | O(N) | Fwd. | ||||
1 | 1 | bP | less |
O(N log(min(D,N)) | ∥ | |||
1 | 2 | uP | =(N) | Fwd.∥ | ||||
1 | 1 | uP, uT | O(N) | Fwd. | ||||
1 | 1 | uP | O(N) | Fwd.∥ | transform_if |
|||
1+Value | 1 | equal_to |
=(N) | Fwd.∥ | transform_if |
|||
1 | First | 1 | bP | equal_to |
O(N) | Fwd.∥ | ||
2 | 1 | bP | less |
O(N+M), ≤(2⋅(N+M)−1) | Fwd.∥ | |||
2 | 1 | bP | less |
O(N+M) | Fwd.∥ | |||
2 | 1 | Regex⧫ | Unspc. | See note⧫ | ||||
3 | 1 | bP | equal_to |
O(N×(S+R)) | regex_ ⧫ |
|||
0 | 1 | G | =(N) | Fwd.∥ | ||||
0 | Arg | 1 | ++ |
=(N) | Fwd. | generate |
||
0 | Arg | 1 | uT | ++ |
=(N) | Fwd. | generate |
|
0 | Arg | 1 | =(N) | Fwd.∥ | generate , generate_n |
|||
1 | 0∅ | muT | =(N) | Fwd.∥ | ||||
1 / 2 | 0∅ | muT / mbT | =(N) | Fwd. | ||||
2 | Self | swap |
O(N) | Fwd.∥ | for_ |
|||
1+Value | Self | equal_to |
O(N) | Fwd.∥ | ||||
1 | Self | uP | O(N) | Fwd.∥ | ||||
1 | Self | bP | equal_to |
O(N) | Fwd.∥ | |||
1+Value | Self | equal_to |
=(N) | Fwd.∥ | transform |
|||
1 | Self | uP | =(N) | Fwd.∥ | transform |
|||
1 | Self | O(N), =(N/2) | Yes∥ | swap_ranges |
||||
1d | Self | O(N) | Yes∥ | |||||
1d | Self | "≤(N-n)" | Fwd.∥ | move (algorithm) |
||||
1d | Self | "≤(N-n)" | Yes∥ | move_ |
||||
1+URBG | Self | O(N) | ||||||
1 | Self | O(N), ≤(N/2) | ||||||
1 | Self | uP | O(N); O(N log N)↓∥ | ∥ | ||||
1 | Self | uP | O(N) w/ memory, else O(N log N); O(N log N)↓∥ | ∥ | ||||
1 | Self | bP | less |
O(N log N) | ∥ | |||
1d | Self | bP | less |
O(N log M) | ∥ | |||
1 | Self | bP | less |
O(N log N) w/ memory, else O(N log(N)2) | ∥ | |||
1d | Self | bP | less |
O(N); O(N log N)↓∥ | ∥ | |||
1+positions | Self | bP | less |
O(N log M) | nth_ |
|||
1 | Self | bP | less |
O(N), ≤(3N) | ||||
1 | Self | bP | less |
O(log N) | ||||
1 | Self | bP | less |
O(N log N) | ||||
1d | Self | bP | less |
=(N-1) w/ memory, O(N log N); O(N log N)↓∥ | ∥ | |||
Name | Input ranges* | Accumulator | Output ranges | Operations† | Default operations | Complexity | Order‡ | Compare to |
Named Operators and Function Objects
Name | Arity | Class (<T>) | Signature (<T>) | Class (<void>) | Definition |
---|---|---|---|---|---|
| 2 | (ac)R | (T, T) → T |
bT | x + y |
minus | 2 | R | (T, T) → T |
bT | x - y |
multiplies | 2 | (ac)R | (T, T) → T |
bT | x * y |
divides | 2 | R | (T, T) → T |
bT | x / y |
modulus | 2 | R | (T, T) → T |
bT | x % y |
negate | 1 | uT | (T) → T |
uT | -x |
equal_to | 2 | (c)bP | (T, T) → bool |
bT | x == y |
not_equal_to | 2 | (c)bP | (T, T) → bool |
bT | x != y |
less | 2 | bP | (T, T) → bool |
bT | x < y |
greater | 2 | bP | (T, T) → bool |
bT | x > y |
less_equal | 2 | bP | (T, T) → bool |
bT | x <= y |
greater_equal | 2 | bP | (T, T) → bool |
bT | x >= y |
logical_and | 2 | (c)bP | (T, T) → bool |
bT | x and y |
logical_or | 2 | (c)bP | (T, T) → bool |
bT | x or y |
logical_not | 1 | uP | (T) → bool |
uT | not x |
bit_and | 2 | (ac)R | (T, T) → T |
bT | x & y |
bit_or | 2 | (ac)R | (T, T) → T |
bT | x | y |
bit_xor | 2 | (ac)R | (T, T) → T |
bT | x ^ y |
bit_not | 1 | uT | (T) → T |
uT | ~x |
Name | Arity | Class (<T>) | Signature (<T>) | Class (<void>) | Definition |
* 's' indicates staggered access to a range. 'd' indicates that a second position in the range is also required.
† The abbreviations used for operations are described below.
Operation types
- A
- Accumulation
(A, T)
→A
- R
- Reduction
(T, T)
→T
- D
- Adjacent Op
(T, T)
→U
- T
- Transform
(Ts...)
→U
- P
- Predicate
(Ts...)
→bool
- G
- Generator
()
→U
Operation semantics
- a
- Associative
op(a, op(b, c))
↔op(op(a, b), c)
- c
- Commutative
op(a, b)
↔op(b, a)
- f
- Flipped
op(b, a)
- u
- Unary
op(a)
- b
- Binary
op(a, b)
- m
- Mutable
op(a)
may modifya
If an operation is specified to be associative (but not commutative), and the actual operation provided is not, the result is potentially non-deterministic, however, it is not UB for any such algorithm, as far as I can tell.
‡ A blank space means unspecified evaluation/traversal order. "S/C" means forward order with short-circuiting (early return). "B/S" means binary search. "Yes" means that an order is specified, but it is not one of the above.
The ∥ mark indicates that an algorithm may execute out-of-order and/or in parallel, and may accept an execution policy. It may be used to indicate that a given time complexity only pertains to the parallel version, see next note.
↓Several algorithms are defined to have different time complexities for different iterator concepts, such as between random access and others, or between forward and input iterators, or anything else. For such algorithms, the computational complexity is marked with an arrow followed by a letter, indicating which iterator category that complexity guarantee applies to. The letter codes are: C for contiguous, R for Random Access, B for Bidirectional, F for Forward, and I for Input. Additionally, the ∥ mark indicates that the given complexity guarantee applies to the parallel version of the algorithm.
Δ This analysis makes no distinction between unary transformations and projections as used in the ranges library, nor does it make any distinction between Compare predicates and general predicates. lexicographical_compare_three_way
is unique in that the Compare predicate it requires must return one of the standard three-way ordering types (strong_ordering
, weak_ordering
, or partial_ordering
), rather than bool
.
⌖ For the purposes of this analysis, returning an iterator, an index, or a pair of corresponding iterators in different ranges (i.e. mismatch
) are all considered to be a single "position". "Range" as a return value of a catamorphism indicates that two iterators identifying a subrange of the input are returned, not that a new range is produced. "2 Positions" means two unassociated iterators are returned, which do not form a range.
∅ I'm not certain if for_each
is, strictly speaking, an anamorphism, because it does not directly produce any output. However, I include it in the anamorphisms table because, as a void
function it doesn't neatly fit into this analysis, and because swap_ranges
, which clearly is an anamorphism, can be implemented in terms of (the binary version of) it.
⧫ regex_replace
is a strange case for this list because it's not really meant to be an algorithm. However, the regular expression can be considered a kind of predicated in exactly the same way as the Searcher of search (C++17)
can be, and the actual operation performed is a kind of conditional two-input copy, in a similar way as set_intersection
et al. are, but with the conditionality being somewhat more like unique_copy
in that it relates to the relationship between nearby elements instead of their individual values. Another unusual thing is that it can return its output as a string by value, in addition to writing to an output iterator. Despite that, I'm considering it an anamorphism because its output is a range of the same kind as its input, even if it is represented in the language as a value type, unlike those catamorphisms which can be used to return ranges, but which, for analytical purposes, return values. I wrote a more generic search_replace_copy
, which is a much more normal entry for this list.
Acknowledgements
This page would not exist without Conor Hoekstra's two-part talk "Algorithm Intuition" (CppCon 2019, parts one and two) to inspire me and provide the basic framework for me to expand upon. Jonathan Boccara's talk "105 STL Algorithms in Less Than an Hour" (CppCon 2018) was also very helpful for me in framing this work.
These presentations themselves reference excellent presentations by Sean Parent ("C++ Seasoning", GoingNative 2013), Marshall Clow ("STL Algorithms - why you should use them, and how to write your own", CppCon 2016), and Kate Gregory ("It's Complicated", Meeting C++ 2017 Keynote, "Simplicity: not just for beginners", ACCU 2018, and her appearance on CppCast, episode 30, "Stop Teaching C (When Teaching C++)").
Top