1 STL Algorithms Several STL algorithms require a functional argument. Consider an employee database. We may want to remove all employees who satisfy the.

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
String class and STL. string class has the following constructors: string (const char *s) //initialize a string object with the string pointed by s string(size_type.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
1 Standard Containers: Lists Gordon College Resource:
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Searching Arrays Linear search Binary search small arrays
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
1 Iterators & Algorithms Iterators provide a link between containers and algorithms Example: We wish to write a function sum() that adds up the members.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Templates and the STL.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
CSE 332: C++ STL functors STL Functors: Functions vs. Function Objects STL Functors support function call syntax Algorithms invoke functions and operators.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
1 Iterators Good reference site:
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
Intro to the C++ STL Timmie Smith September 6, 2001.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
Algorithms CNS 3370 Copyright 2003, Fresh Sources, Inc.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Kruse/Ryba Ch071 Object Oriented Data Structures Searching STL Vector Sequential Search Binary Search Comparison Trees.
Washington WASHINGTON UNIVERSITY IN ST LOUIS More on the STL: Function Objects and Generic Algorithms in the STL Fred Kuhns Computer Science and Engineering.
Searching Arrays Linear search Binary search small arrays
Lecture 36 OOP The STL Standard Template Library
Chapter 18 - C++ Operator Overloading
CS212: Object Oriented Analysis and Design
Standard Template Library
Section 3 Review Mr. Crone.
Vectors Holds a set of elements, like an array
Chapter 8 Arrays, Strings and pointers
Multiple Files Revisited
Associative Structures
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
14 – Sequential Containers
Searching and Sorting Arrays
CS212: Object Oriented Analysis and Design
Lesson Subject Book Week 4 lesson 1 Class, copy-constructor
Data Structures and Algorithm: SEARCHING TECHNIQUES
CS150 Introduction to Computer Science 1
the Standard Template Library
Jim Fawcett CSE687 – Object Oriented Design Spring 2002
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
The List Container and Iterators
Standard Template Library
Presentation transcript:

1 STL Algorithms Several STL algorithms require a functional argument. Consider an employee database. We may want to remove all employees who satisfy the requirement "the last evaluation report was very bad". A suitable function would be something like: remove_if (first, last, bad_eval(t)) where bad_eval(t) is a function 1 that examines an element's evaluation number and returns T if it is below some threshold t. Or, we may want to sort all employees based on their evaluation number. We will then need to do something like sort(first, last, greaterThan()) where greaterThan() defines a comparison between employees, based on their evaluations. 1 Functions that return T or F are called predicates.

2 class Employee { private: int eval_number; public: Employee(int en=0) : eval_number(en) {}; int get_evaluation() const {return eval_number;} int set_evaluation(int num) {eval_number = num;} }; // We'd now like to do this: vector db(10); for (int i=0; i<10; i++) { db[i].set_evaluation(rand()%20); } sort(db.begin(), db.end(), LessThan); LessThan must be a strict weak ordering: A Strict Weak Ordering is a Binary Predicate that compares two objects, returning true if the first precedes the second. Example 1

3 class Employee { private: int eval_number; public: Employee(int en=0) : eval_number(en) {}; int get_evaluation() const {return eval_number;} int set_evaluation(int num) {eval_number = num;} }; bool LessThan(const Employee& first, const Employee& second) { return (first.get_evaluation() < second.get_evaluation()); }; vector db(10); for (int i=0; i<10; i++) { db[i].set_evaluation(rand()%20); } sort(db.begin(), db.end(), LessThan); A predicate is a function that returns true or false Result: the elements within the specified range have been sorted from smaller to larger based on the evaluation number A binary predicate is a function with two input arguments Example 1

4 class Employee { private: int eval_number; public: Employee(int en=0) : eval_number(en) {}; int get_evaluation() const {return eval_number;} int set_evaluation(int num) {eval_number = num;} }; // We'd now like to do this: vector db(10); for (int i=0; i<10; i++) { db[i].set_evaluation(rand()%20); } remove_if(db.begin(), db.end(), LessThan4); LessThan10 must be a Predicate A Predicate is a unary function that returns true or false. Example 2 In this case, it will return true when the employee's evaluation number is less than 4.

5 class Employee { private: int eval_number; public: Employee(int en=0) : eval_number(en) {}; int get_evaluation() const {return eval_number;} int set_evaluation(int num) {eval_number = num;} }; bool LessThan4(const Employee& empl) { return (empl.get_evaluation() < 4); }; vector db(10); for (int i=0; i<10; i++) { db[i].set_evaluation(rand()%20); } remove_if(db.begin(), db.end(), LessThan4); A predicate is a function that returns true or false A unary predicate is a function with one input argument Example 2

6 vector db(10); for (int i=0; i<10; i++) { db[i].set_evaluation(i); } vector ::iterator new_end; new_end = remove_if(db.begin(), db.end(), LessThan4); db.erase(new_end, db.end()); Notes about remove_if: It "removes" elements but does not change the size of the sequence. It returns an iterator to the new end of the sequence. We must explicitly erase the elements between the new and the old end of the sequence. Example 2 Here, db is Here, db is Here, db is

7 // We'd now like to do this: vector db(10); for (int i=0; i<10; i++) { db[i].set_evaluation(rand()%20); } remove_if(db.begin(), db.end(), LessThan(6)); Example 3 It is much more useful to be able to specify a certain threshold and then remove all employees whose evaluation number is less than that threshold. For something like this to work, the LessThan function must be able to "hold" data. A member function of a class works better here because its object can hold data. An object of a class with an application operator (i.e. operator() ) is called a function object.

8 class Employee { private: int eval_number; public: Employee(int en=0) : eval_number(en) {}; int get_evaluation() const {return eval_number;} int set_evaluation(int num) {eval_number = num;} }; class LessThan { private: int threshold; public: LessThan(int val=0) : threshold(val) {} bool operator() (Employee em) { return em.get_evaluation() < threshold; } }; vector db(10); for (int i=0; i<10; i++) { db[i].set_evaluation(rand()%20); } remove_if(db.begin(), db.end(), LessThan(6)); Example 3