CPP04-STLAlgorithms 4up PDF
CPP04-STLAlgorithms 4up PDF
http://cs.stmarys.ca/~porter/csc/ref/stl/index_algorithms.html Contents
Stanford 106L, Standard C++ Programming Laboratory
http://web.stanford.edu/class/cs106l/
1. Why STL algorithm? 11. Mapping algorithms
topcoder’s tutorial, Power up C++ with the STL, part I and II
https://www.topcoder.com/community/data-science/data-science-tutorials/ 2. Accumulate 12. Palindrome
power-up-c-with-the-standard-template-library-part-1/
3. STL algorithm naming 13. Utilities - ctype and math
STL Algorithms 4. Iterator categories 14. Magic square
5. Reordering algorithms 15. Substitution cipher
<algorithm>, <numeric>, <iterator>, <functional>
6. Searching algorithms 16. Graph connectivity –
<cctype>, <cmath>
7. Iterator adaptors DFS and BFS
C++, a multi-paradigm programming language,
8. Removal algorithms 17. Dijkstra’s Shortest Path
besides being procedural and object-oriented,
is very much functional with STL 9. Functional Thinking
Pei-yih Ting
10. Optimized machinery: Map / Filter / Reduce
NTOU CS
1 2
Map: accumulate sums up the elements in a range and returns the result
initial value
transform / copy / for_each / replace / sort / partition multiset<int> values; [begin(), end())
Filter: …
removal (find and erase)
double total = accumulate(values.begin(), values.end(), 0.0);
Reduce: accumulate(values.lower_bound(42), values.upperbound(99), 0.0);
accumulate / min_element / count / equal / search / selection Your first higher order function (user customized)
accumulate is a general-purpose function for transforming a
customized with callable objects collection of elements into a single value (in functional language
(functions and functors) terms: reduce / collect / convert / join / fold)
int findLess(int smallestSoFar, int current) {
Core data structure: container return current < smallestSoFar ? current : smallestSoFar; }
int smallest = accumulate(values.begin(), values.end(),
5 <limits> numeric_limits<int>::max(), findLess); 6
#include <queue>
Max Heap std::priority_queue<int>
push()
Searching Algorithms
Maintain a max heap in a vector or deque empty(), top(), pop() InputItr find(InputItr first, InputItr last, const Type& value)
creation (heapify): make_heap // random-access iterators, operator< search for value in designated range [first, last)
extract maximum: pop_heap return an iterator to the first element found; use a while loop to find all
does not remove maximum element from the container returns last iterator if nothing found
move the maximum to the end of the range, use v.pop_back() to remove it if (find(myVec.begin(), myVec.end(), 137) != myVec.end()) …
does not return anything, use v.front() beforehand or use v.back() afterward
avoid using find() on set or map, use set::find() or map::find() for efficiency
insert element: push_heap
1. v.push_back() to append the element, 2. push_heap() to sift-up 30 20 10 15 5 iterator_traits<InputItr::difference_type> count(InputItr first,
sort the heap: sort_heap 10 30 InputItr last, const Type& value)
return the number of elements x == value in the designated range [first, last)
int myints[] = {10,20,30,15,5}; 20 30 20 10
vector<int> v(myints,myints+5); 15 5 15 5 bool binary_search(RandItr first, RandItr last,
make_heap(v.begin(),v.end()); const Type& value)
cout << v.front() << endl; // 30 20 search for value in the designated sorted range, [first, last), delineating by two
20
pop_heap(v.begin(),v.end()); v.pop_back(); random-access iterators, i.e. iterators of vector or deque (map or set are sorted,
15 10
17 10 their find() are efficient, i.e. log n)
v.push_back(17); push_heap(v.begin(),v.end()) 5 20 15 10 5 return true if found; false otherwise
5 15
sort_heap(v.begin(),v.end()); // no longer a heap if (binary_search(myVec.begin(), myVec.end(), 137)) … // found
20 17 10 5 15 15 16
Searching Algorithms (cont’d) Searching Algorithms (cont’d)
ForwardItr lower_bound(ForwardItr first, ForwardItr last, ForwardItr search(ForwardItr first1, ForwardItr last1,
const Type& value) ForwardItr first2, ForwardItr last2)
Find the first element x value in the designated sorted range [first, last) Searches the range [first1, last1) for the first occurrence of the subsequence
itr = lower_bound(myVec.begin(), myVec.end(), 137); defined by [first2, last2), operator== is required
return an iterator to the first element satisfying x value returns an iterator to its first element in [first1, last1), or last1 if no occurrence
if (itr == last) … // all elements in [first, last) satisfy x < value is found.
else if (*itr == 137) … // 137 is found e.g. [first1, last1) = (10, 20, 30, 40, 50, 60, 70, 80)
else … // *itr > 137 [first2, last2) = (40, 50, 60, 70)
Invoking search(first1, last1, first2, last2) returns first1+3
ForwardItr upper_bound(ForwardItr first, ForwardItr last, ForwardItr search_n(ForwardItr first, ForwardItr last,
const Type& value) Size n, const Type& value) find first subsequence of n value’s
Find the first element x > value in the designated sorted range [first, last)
bool includes(InItr first1, InItr last1, InItr first2, InItr last2)
itr = upper_bound(myVec.begin(), myVec.end(), 137);
Two sorted ranges [first1, last1), [first2, last2)
return an iterator to the first element satisfying x > value
Returns whether every elements in [first2, last2) is also in [first1, last1)
both algorithms are O(log n) e.g. (1,2,3,4,5) includes (2,4)
17 18