90 likes | 341 Views
STL Algorithms. Examples of how to use some STL algorithms. 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?. Finding Elements.
E N D
STL Algorithms Examples of how to use some STL algorithms
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?
Finding Elements • Common scenario: • 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)
Finding Elements – Syntax #include <algorithm> #include <iostream> #include <list> #include <vector> intmain(){ 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 deterinetype Search the entire list If find()fails, it will return collection.end() simple_find.cpp
Exercise: Using iterators and find #include <algorithm> #include <iostream> #include <iterator> #include <vector> intmain(){ std::vector<int> collection ={1,1,3,4}; autoconstfirst_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. first_element_is_one.cpp
Some examples from #include <algorithm> http://en.cppreference.com/w/cpp/algorithm
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))
Exercise: Sort and Print a Vector #include <algorithm> #include <iostream> #include <vector> intmain(){ std::vector<int> numbers ={5,8,2,4,1,9,12}; std::sort(numbers.begin(),numbers.end());// O(n log(n)) for(autoconst 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
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