# 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.

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. 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.

Home## Catamorphisms (folds) and Searches

Name | Input ranges* | Accumulator | Returns⌖ | Operations† | Default operations | Order‡ | Compare to | Family |
---|---|---|---|---|---|---|---|---|

`inner_` |
2 | Arg | Value | A, bT | `plus` , `multiplies` | Fwd. | 10 | |

`adjacent_` |
1s | Arg | Value | A, bT | Fwd. | `inner_` | 10 | |

`transform_` |
1 / 2 | Arg | Value | acR, uT / bT | `plus` , `multiplies` | ∥ | 20 | |

`find` |
1+Value | Position | `equal_to` | S/C∥ | 70 | |||

`find_if` , `find_` |
1 | Position | uP | S/C∥ | 70 | |||

`find_` |
2 | Position | bP | `equal_to` | S/C∥ | `find_if` | 70 | |

`min_` , `max_` |
1 | First | Position | bP | `less` | Fwd.∥ | 72 | |

`minmax_` |
1 | First | 2 Positions⌖ | bP | `less` | Fwd.∥ | 72 | |

`lower_` , `upper_` |
1+Value | Position | bP | `less` | B/S | 75 | ||

`equal_` |
1+Value | Range⌖ | bP | `less` | B/S | 75 | ||

`search` , `search_n` , `find_end` |
2 | Position | bP | `equal_to` | ∥ | 80 | ||

`starts_` |
2 | `bool` | bP | `equal_to` | Fwd. | 80 | ||

`ends_` |
2 | `bool` | bP | `equal_to` | 80 | |||

`ranges::` , `ranges::` |
2 | `bool` | bP, uT, uTΔ | `equal_to` , `identity` , `identity` | 80 | |||

`search (C++17)` |
1 | Position | Searcher | 85 | ||||

`find_` |
2 | Position⌖ | bP | `equal_to` | S/C | 90 | ||

`mismatch` |
2 | Position⌖ | bP | `equal_to` | S/C∥ | `find_match` | 90 | |

`adjacent_` |
1s | Position | bP | `equal_to` | S/C∥ | `find_match` | 90 | |

`is_sorted_` |
1s | Position | bP | `less` | S/C∥ | `find_match` | 90 | |

`accumulate` |
1 | Arg | Value | A | `plus` | Fwd. | 110 | |

`sum` |
1 | First | Value | A | `plus` | Fwd. | `accumulate` | 110 |

`reduce` |
1 | Arg | Value | acR | `plus` | ∥ | 110 | |

`count` |
1+Value | `0` | `size_t` | `equal_to` | Fwd.∥ | `accumulate` | 110 | |

`count_if` |
1 | `0` | `size_t` | uP | Fwd.∥ | `accumulate` | 110 | |

`binary_` |
1+Value | `bool` | bP | `less` | B/S | 110 | ||

`is_partitioned` |
1 | `bool` | uP | Fwd.∥ | 120 | |||

`is_sorted` |
1 | `bool` | bP | `less` | Fwd.∥ | 120 | ||

`is_heap` |
1 | `bool` | bP | `less` | ∥ | 120 | ||

`is_permutation` |
2 | `bool` | bP | `equal_to` | 120 | |||

`includes` |
2 | `bool` | bP | `less` | Fwd.∥ | 120 | ||

`first_result` |
1+Value / 2+Value | Value | uT / bT | S/C | 125 | |||

`all_of` , `none_of` |
1 | `true` | `bool` | uP | S/C∥ | `first_result` | 125 | |

`any_of` |
1 | `false` | `bool` | uP | S/C∥ | `first_result` | 125 | |

`contains` |
1+Value | `false` | `bool` | `equal_to` | S/C | `first_result` | 125 | |

`equal` |
2 | `bool` | bP | `equal_to` | Fwd.∥ | `first_result` | 125 | |

`lexicographical_` |
2 | `bool` | bP | `less` | Fwd.∥ | `first_result` | 125 | |

`lexicographical_` |
2 | ordering | CompareΔ | `compare_` | Fwd. | `first_result` | 125 | |

`partition_` |
1 | Position | uP | B/S | 130 | |||

`is_heap_` |
1 | Position | bP | `less` | ∥ | 130 | ||

Name | Input ranges* | Accumulator | Returns⌖ | Operations† | Default operations | Order‡ | Compare to | Family |

## Anamorphisms (unfolds/transforms)

Name | Input ranges* | Accumulator | Output ranges | Operations† | Default operations | Order‡ | Compare to | Family |
---|---|---|---|---|---|---|---|---|

`transform_` |
1 | First, Arg | 1 | aR, uT | ∥ | 20 | ||

`transform_` |
1 | Arg | 1 | aR, uT | ∥ | 20 | ||

`partial_` |
1 | First | 1 | A | `plus` | Fwd. | 30 | |

`inclusive_` |
1 | First, Arg | 1 | aR | `plus` | ∥ | 30 | |

`exclusive_` |
1 | Arg | 1 | aR | `plus` | ∥ | 30 | |

`adjacent_` |
1s | Quasi | 1 | fD | `minus` | Fwd.∥ | `adjacent_` | 40 |

`adjacent_` |
1s | Quasi | 1 | D | Fwd.∥ | `adjacent_` | 40 | |

`adjacent_` |
1s | First | 1 | A, D | Fwd. | 50 | ||

`transform` |
1 / 2 | 1 | uT / bT | ∥ | 60 | |||

`copy` , `copy_n` |
1 | 1 | Fwd.∥ | `transform` | 60 | |||

`copy_` |
1 | 1 | Rev. | 60 | ||||

`move` |
1 | 1 | Fwd.∥ | `shift_left` | 60 | |||

`move_` |
1 | 1 | Rev. | `shift_` | 60 | |||

`replace_` |
1+Value | Arg | 1 | `equal_to` | Fwd.∥ | `transform` | 60 | |

`replace_` |
1 | Arg | 1 | uP | Fwd.∥ | `transform` | 60 | |

`reverse_` |
1 | 1 | ∥ | `transform` | 60 | |||

`rotate_` |
1d | 1 | ∥ | 60 | ||||

`sample` |
1 | Arg | 1 | Fwd. | 60 | |||

`partial_` |
1 | 1 | bP | `less` | ∥ | 60 | ||

`merge` |
2 | 1 | bP | `less` | Fwd.∥ | 60 | ||

`partition_` |
1 | 2 | uP | Fwd.∥ | 63 | |||

`transform_if` |
1 | 1 | uP, uT | Fwd. | 65 | |||

`copy_if` , `remove_` |
1 | 1 | uP | Fwd.∥ | `transform_if` | 65 | ||

`remove_` |
1+Value | 1 | `equal_to` | Fwd.∥ | `transform_if` | 65 | ||

`unique_` |
1 | First | 1 | bP | `equal_to` | Fwd.∥ | 65 | |

`set_` , `set_` , `set_` , `set_` |
2 | 1 | bP | `less` | Fwd.∥ | 67 | ||

`regex_` |
2 | 1 | Regex⧫ | See note⧫ | 68 | |||

`search_` |
3 | 1 | bP | `equal_to` | `regex_` ⧫ | 68 | ||

`generate` , `generate_n` |
0 | 1 | G | Fwd.∥ | 100 | |||

`iota` |
0 | Arg | 1 | `++` | Fwd. | `generate` | 100 | |

`iota` |
0 | Arg | 1 | uT | `++` | Fwd. | `generate` | 100 |

`fill` , `fill_n` |
0 | Arg | 1 | Fwd.∥ | `generate` , `generate_n` | 100 | ||

`for_` , `for_` |
1 | 0∅ | muT | Fwd.∥ | 140 | |||

`for_` , `for_` |
1 / 2 | 0∅ | muT / mbT | Fwd. | 140 | |||

`swap_` |
2 | Self | `swap` | Fwd.∥ | `for_` | 140 | ||

`remove` |
1+Value | Self | `equal_to` | Fwd.∥ | 150 | |||

`remove_if` |
1 | Self | uP | Fwd.∥ | 150 | |||

`unique` |
1 | Self | bP | `equal_to` | Fwd.∥ | 150 | ||

`replace` |
1+Value | Self | `equal_to` | Fwd.∥ | `transform` | 160 | ||

`replace_if` |
1 | Self | uP | Fwd.∥ | `transform` | 160 | ||

`reverse` |
1 | Self | Yes∥ | `swap_ranges` | 170 | |||

`rotate` |
1d | Self | Yes∥ | 170 | ||||

`shift_` |
1d | Self | Fwd.∥ | `move` | 170 | |||

`shift_` |
1d | Self | Yes∥ | `move_` | 170 | |||

`shuffle` |
1+URBG | Self | 170 | |||||

`next_` , `prev_` |
1 | Self | 170 | |||||

`partition` , `stable_` |
1 | Self | uP | ∥ | 170 | |||

`sort` , `partial_` , `stable_` , `nth_` |
1 | Self | bP | `less` | ∥ | 170 | ||

`make_` , `push_` , `pop_` , `sort_` |
1 | Self | bP | `less` | 170 | |||

`inplace_` |
1d | Self | bP | `less` | ∥ | 170 | ||

Name | Input ranges* | Accumulator | Output ranges | Operations† | Default operations | Order‡ | Compare to | Family |

* '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 modify`a`

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.

Δ 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