Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 STL Algorithms Several STL algorithms require a functional argument. Consider an employee database. We may want to remove all employees who satisfy the."— Presentation transcript:

1 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 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 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 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 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 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 0 1 2 3 4 5 6 7 8 9 Here, db is 4 5 6 7 8 9 6 7 8 9 Here, db is 4 5 6 7 8 9

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


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

Similar presentations


Ads by Google