Download presentation
Presentation is loading. Please wait.
Published byJasmin Baldwin Modified over 6 years ago
1
STL Algorithms Examples of how to use some STL algorithms
2
Today’s Questions What algorithms are provided by the STL?
How can I find a specific value in a container? How can I sort a container? How can I find the information I need to use the STL on my own?
3
Finding Elements Common scenario: Option 1 Option 2
You have a collection of data You need to find a specific value in the collection Option 1 Write a for loop to go through each element On each iteration, compare the current value with what you’re searching for Finish when you have found the element Option 2 Use std::find Give an iterator to the beginning and end of the container Returns iterator to first match …is more idiomatic …requires less typing (and thus, less reading) …dictates intent (to find a value) in the name of the function …re-uses well-tested code (less prone to programmer error)
4
Finding Elements – Syntax
#include <algorithm> #include <iostream> #include <list> #include <vector> int main() { std::list<int> collection = {1, 2, 3, 4}; auto it = std::find(collection.begin(), collection.end(), 3); if(it != collection.end()) { std::cout << "Found " << *it << "!\n"; } Compiler will deterine type Search the entire list Animate me in order If find() fails, it will return collection.end() simple_find.cpp
5
Exercise: Using iterators and find
#include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> collection = {1, 1, 3, 4}; auto const first_element = collection.begin(); auto it = std::find(first_element + 1, collection.end(), 1); if(*first_element == 1 && it == collection.end()) { std::cout << "1 only exists as the first element\n"; } else { std::cout << "1 exists outside of the first element\n"; } Using std::find and iterators, ensure that only the first element is 1. Hint to students the algorithm: * Check if first element is 1 * Check remaining elements for any ‘1’s first_element_is_one.cpp
6
Some examples from #include <algorithm>
Function Algorithm Description std::min Returns the smaller of two given values std::max Returns the larger of two given values std::sort Sort a range (e.g. vector) into ascending order std::find Find the first element of a given value in a range (start and end iterators) std::swap Swap the values of two objects std::count Count the number of elements of a given value in a range (start and end iterators) And many, many more! All templated for your convenience.
7
Exercise: Find the std::sort function online
Use your phone/laptop – I’ll wait How many arguments does std::sort accept? The simplest one requires two, both are the same type What are the requirements placed on the arguments? They must be RandomAccessIterators How are elements compared? In the simplest std::sort function, they are compared with operator< What is the complexity of std::sort? O(n log(n))
8
Exercise: Sort and Print a Vector
#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> numbers = {5, 8, 2, 4, 1, 9, 12}; std::sort(numbers.begin(), numbers.end()); // O(n log(n)) for(auto const it = numbers.begin(); it != numbers.end(); ++it) { std::cout << *it << " "; } std::cout << "\n; Use std::sort, then print the numbers to std::cout sort_print_vector.cpp
9
Course Expectations The Standard Template Library is massive
We can’t teach it all to you But it is, and will be, very useful Browse what the STL has to offer and use it in ECE297 Lots of great examples online Read, read, read first. Don’t skip to the examples. Check out ideone.com to quickly try some C++ code Ask your favourite TAs on piazza for more help
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.