Download presentation
Presentation is loading. Please wait.
1
STL - Algorithms
2
Imperative vs Declarative
Imperative : Do this, then that
3
Imperative vs Declarative
Declarative : Here is what I would like to see
4
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
5
Enhanced For Can use with arrays/strings
Declare temp var as & to modify
6
#include <algorithm>
Nonmodifying: algorithms Use iterators to do work to collection
7
Locators Locating functions return iterator Dereference to get item
Subtract starting location to get index
8
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
9
UnaryPredicate??? Some algorithms require helper functions to do work
count_if needs function to be the "if"
10
Count If Odd Function name without () = address of function
11
for_each for_each applies function to range
12
for_each Use reference parameter to modify values as you go:
13
Lambdas Lambda See: Anonymous function written inside code
Eliminate need for formally defining one of function See:
14
Accumulator Accumulate(start, end, value, op )
Do op (default +) to combine each value from start to end with value
15
Built In Functions Op function can be own: Or a std:: one
16
Modifiers Algorithms that modify containers:
17
Sort Sort using natural ordering ( < )
18
Sort Change sort criteria with sort function:
Take two things, return true if first one goes first Sort using greaterCompare to order:
19
Sort Criteria Different sort functions for different sorts
Sort by age descending: Sort by name ascending:
20
Remove Remove doesn't actually remove
21
Remove Remove effect Move values to left to "remove" items
Returns iterator pointing to new end of data
22
Arrays as Targets Can use many algorithms… On arrays On strings
Use pointers as iterators On strings Strings have .begin() and .end()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.