29 Oct 2026
C++ STL Algorithm Notes
Day 1
WIP!
I can't believe ive been writing these by hand for years in Python
-
find_if is O(n) but partition_point is O(log n). Because it does the same "find first instance where" search but assumes the array is already partioned.
-
you can use combination keywords to get variations of the same algo
e.g. is_sorted, is_partioned, is_heap
or the classics like stable_sort, stable_partition
is_sorted_until, is_partioned_until, is_heap_until -
the parallelisable verison of accumulate is reduce
the parallelisable version of partial_sum is inclusive_scan/exclusive_scan
they're both the versions where you can make transform_*s upon the array before performing whatever monstrosity you want to do -
mismatch returns the first index where the two lists aren't alike anymore. It returns a pair of iterators
-
this poor naming convention
-
anything thats sorted is a set in c++
std::set_difference(begin(A), end(A),
begin(B), end(B),
std::back_inserter(results));
- i can use std::move on a whole collection simply by overloadin git
std::move(begin(col), end(col), std::back_inserter(out));
-
if modifying inplace and needing your copy in the other direction, consider using copy_backwards/move_backwards
-
you need to wrap remove and unique in this erase function call
collection.erase(std::unique(begin(collection), end(colllection)), end(collection));
- std::transform save me from my stupidity
std::transform( begin(col), end(col), std::back_inserter(res), f );
//or if you would like 2...
std::transform( begin(col), end(col), begin(ys), std::back_inserter(res), f );
- for_each is the side effect version of std::transform. We don't shove the answers