SLIDE 5 Abridged Catalogue of algorithms
– Fills or a range with a particular value (constant or generated) – fill, fill_n, generate, generate_n
– count, count_if (counts elements w/a given value)
– copy, reverse, swap, random_shuffle
Abridged Catalogue of algorithms
– find, find_if, find_first_of, replace, replace_if
- Takes predicate as argument! FUNCTOR
– max_element, min_element – search (range searches)
– equal, mismatch
– remove, unique (removes duplicates)
Abridged Catalogue of algorithms
– sort, partial_sort, nth_element – binary_search, lower_bound, upper_bound – merge – set_union, set_difference, set_intersection
- Applying an operation to each element
– for_each, transform
- Takes operation as argument! FUNCTOR!
- Numeric Algorithms
– Accumulate, partial_sum
Functors
- Function Objects (or Functors)
– Objects that represent functions – Overrides operator() – Functors are Templated – But why bother when you have functions?
map<string, int, less<string> > mymap;
Functors
template <class T> struct less : public binary_function<T, T, bool> { bool operator() (const T &x, const T &y) const { return x < y; } } Note: will only work if < is defined for T.
Functors
- STL Predicates (returns bool)
!arg logical_not arg1 || arg2 logical_or arg1 && arg2 logical_and arg1 <= arg2 less_equal arg1 >= arg2 greater_equal arg1 < arg2 less arg1 > arg2 greater arg1 != arg2 not_equal_to arg1 == arg2 equal_to