STL - Algorithms
Imperative vs Declarative Imperative : Do this, then that
Imperative vs Declarative Declarative : Here is what I would like to see
Enhanced For "Enhanced For loop" For loop that does not use indexes Go through vec, grab each element in turn, store it in value and then do this to it
Enhanced For Can use with arrays/strings Declare temp var as & to modify
#include <algorithm> Nonmodifying: algorithms Use iterators to do work to collection
Locators Locating functions return iterator Dereference to get item Subtract starting location to get index
Comparisons == on STL containers tests if contents exactly same equals compares ranges Start at Start1, Go up to End1 Match with things starting at Start 2
UnaryPredicate??? Some algorithms require helper functions to do work count_if needs function to be the "if"
Count If Odd Function name without () = address of function
for_each for_each applies function to range
for_each Use reference parameter to modify values as you go:
Lambdas Lambda See: Anonymous function written inside code Eliminate need for formally defining one of function See: http://www.cprogramming.com/c++11/c++11-lambda-closures.html
Accumulator Accumulate(start, end, value, op ) Do op (default +) to combine each value from start to end with value
Built In Functions Op function can be own: Or a std:: one
Modifiers Algorithms that modify containers:
Sort Sort using natural ordering ( < )
Sort Change sort criteria with sort function: Take two things, return true if first one goes first Sort using greaterCompare to order:
Sort Criteria Different sort functions for different sorts Sort by age descending: Sort by name ascending:
Remove Remove doesn't actually remove
Remove Remove effect Move values to left to "remove" items Returns iterator pointing to new end of data
Arrays as Targets Can use many algorithms… On arrays On strings Use pointers as iterators On strings Strings have .begin() and .end()