STL Algorithms Examples of how to use some STL algorithms.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Starting Out with C++, 3 rd Edition 1 Chapter 16 – Exceptions, Templates, and the Standard Template Library (STL) Exceptions are used to signal errors.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
C++ for Engineers and Scientists Third Edition
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Templates and the STL.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
Chapter 16: Searching, Sorting, and the vector Type.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Intro to the C++ STL Timmie Smith September 6, 2001.
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Motivation for Generic Programming in C++
Chapter 16: Searching, Sorting, and the vector Type
Module 20/21/22: The Standard Template Library
C++ Standard Template Library
C++ Memory Management – Homework Exercises
C++ Lesson 1.
Chapter 6 CS 3370 – C++ Functions.
C++ Programming Standard Template Library Homework 5,6,7.
Programming with ANSI C ++
Searching Given a collection and an element (key) to find… Output
Recitation 13 Searching and Sorting.
STL Common tools for C++.
C++ Programming Standard Library
Generic Programming Techniques in C++
C++ Templates L03 - Iterator 10 – Iterator.
Templates.
Binary search.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CS212: Object Oriented Analysis and Design
CMSC 202 Lesson 22 Templates I.
MSIS 655 Advanced Business Applications Programming
Functions In Matlab.
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
STL Iterators Separating Container from Data Access.
The Standard Template Library
STL Containers Some other containers in the STL.
Standard Version of Starting Out with C++, 4th Edition
Search algorithms for vectors
Iterators and STL Containers
Exceptions, Templates, and the Standard Template Library (STL)
Templates I CMSC 202.
Functionality & Performance in Milestone 1
Algorithmic complexity
Templates.
C++, Sorting, Convex Hull
Generic Set Algorithms
Presentation transcript:

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: 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)

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

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

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. 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> 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

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