Sign in with Google to keep private notes

C++ STL Algorithm Notes

Day 1

WIP!
I can't believe ive been writing these by hand for years in Python

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

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

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

  4. mismatch returns the first index where the two lists aren't alike anymore. It returns a pair of iterators

  5. this poor naming convention

  6. anything thats sorted is a set in c++

std::set_difference(begin(A), end(A),
                    begin(B), end(B),
                    std::back_inserter(results));
  1. i can use std::move on a whole collection simply by overloadin git
std::move(begin(col), end(col), std::back_inserter(out));
  1. if modifying inplace and needing your copy in the other direction, consider using copy_backwards/move_backwards

  2. you need to wrap remove and unique in this erase function call

collection.erase(std::unique(begin(collection), end(colllection)), end(collection));
  1. 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 );
  1. for_each is the side effect version of std::transform. We don't shove the answers

Articles to read next