1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| #include <**algorithm**>
// ═════╣ **Frequent Methods ╠═════ \\**
**sort**(start_iter, end_iter); // Works with strings as well.
**stable_sort**(start_iter, end_iter);
**fill**(start_iter, end_iter, 10); // Fills a vector range with 10s
// Note: vectors give you a constructor that lets you fill it with values.
// This is mainly useful for arrays.
**find**(start_iter, end_iter, target_value); // Note: maps, sets, etc. would have their own find method.
// These are useful when working with vectors.
**find_if**(start_iter, end_iter, predicate); // Where predicate is a function that takes in an element of the
// container and returns true/false for whether it should be returned.
// `find_if` returns an iterator.
**iter_swap**(start_iter, end_iter) // Swaps the element at the given iterator positions. Saves you some time
// from writing a swap function yourself.
// ═════╣ **Functional Programming ╠═════ \\**
vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
vector<int> u;
// **Map**
**transform**(v.begin(), v.end(), **back_inserter(u)**, **[[]] { return val + 1; }**);
// **Filter**
**copy_if**(v.begin(), v.end(), **back_inserter(u)**, **[[]] { return val % 2 == 0; }**);
// Note: for **reduce**, see either **std::accumulate** or **std::reduce**.
// ═════╣ **Binary Search ╠═════ \\**
// These should be executed on **sorted** ranges.
**lower_bound**(start_iter, end_iter, target) // If `target` exists, returns the index of `target`.
// Else, return the index where we would expect it to be.
// Ie. this function gives you the index of the **greatest value less than `target`**.
**upper_bound**(start_iter, end_iter, target) // If `target` exists, returns the index of `target` + 1.
// Else, return the index where we would expect it to be.
// Ie. this function gives you the index of the **smallest value greater than `target`**.
|