values; double total = 0.0; for (multiset Download presentation Presentation is loading. Please wait. Published byἐλπίς Κωνσταντόπουλος
Modified over 6 years ago
1
CS212: Object Oriented Analysis and Design
2
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;
3
Discussion Intent In practice
4
Accumulate Averaging program Using “accumulate” algorithm
5
More on “accumulate” Defined in the <numeric> header
6
Do we really need them? There are reasons behind using STL algorithms
7
Reordering algorithms
8
Searching Algorithms Checking membership in a container
9
Removal Algorithms For removing elements from containers
10
Removal algorithm: example
Similar presentations © 2025 SlidePlayer.com. Inc. Log in
CS212: Object Oriented Analysis and Design
Similar presentations
Presentation on theme: "CS212: Object Oriented Analysis and Design"— Presentation transcript:
STL Algorithms
“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”
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;
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);
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
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)
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 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
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)
Similar presentations
All rights reserved.