CS212: Object Oriented Analysis and Design STL Algorithms
Introduction Read the content of the file Add the values together Divide by number of elements Problem statement: Suppose that we want to write a program that reads in a list of integers from a file (data.txt), then prints out the average of those values ifstream input("data.txt"); multiset<int> values; double total = 0.0; for (multiset<int>::iterator itr = values.begin(); itr != values.end(); ++itr) total += *itr; int currValue; while (input >> currValue) values.insert(currValue); cout << "Average is: " << total / values.size() << endl;
Discussion Intent In practice “read the contents of the file into the multiset“ “sum the elements together” “create an integer, and then while it's possible to read another element out of the file, do so and insert it into the multiset.” “initialize the total to zero, then iterate over the elements of the multiset, increasing the total by the value of the current element at each step.” “Mechanical model”
Accumulate Averaging program Using “accumulate” algorithm double total = 0.0; for (multiset::iterator itr = values.begin(); itr != values.end(); ++itr) total += *itr; cout << "Average is: " << total / values.size() << endl; Using “accumulate” algorithm cout << accumulate(values.begin(), values.end(), 0.0) / values.size() << endl;
More on “accumulate” Defined in the <numeric> header Three parameters (arguments) are passed to it Iterator 1: Beginning of the range of values Iterator 2: End of the range of values Initial value to be used in the summation Demonstration (AccumSum.cpp) template <class InputIterator, class T> T accumulate (InputIterator first, InputIterator last, T init);
Do we really need them? There are reasons behind using STL algorithms Simplicity Leverage existing codes A great time-saver Correctness Reduces the chance of slip up and making mistakes Speed Optimized to work as fast as possible Clarity Ease of understanding of existing modules
Reordering algorithms Reorder but preserve the elements in a container sort(RandomAccessIterator first, RandomAccessIterator last ); random_shuffle(RandomAccessIterator first, RandomAccessIterator last)); rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last); Demonstration (ReorderAlgo.cpp)
Searching Algorithms Checking membership in a container InputIterator find (InputIterator first, InputIterator last, const T& val); Returns an iterator to the first element in the range that compares equal to val If no such element is found, the function returns last. bool binary_search (ForwardIterator first, ForwardIterator last, const T& val); The elements in the range shall already be sorted Demonstration (SearchAlgo.cpp)
Removal Algorithms For removing elements from containers Removal algorithms do not actually remove elements from containers Algorithms accept iterators, not containers Do not know how to erase elements from containers Removal functions work by shuffling down the contents of the container
Removal algorithm: example 1 2 3 4 3 ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val); The removal is done by replacing the elements that compare equal to val by the next element that does not F L 1 2 3 4 4 == == == == == == == 3 3 3 3 3 3 3 Demonstration (RemoveAlgo.cpp)