Download presentation
Presentation is loading. Please wait.
Published byClaude Thomas Modified over 9 years ago
1
CS212: Object Oriented Analysis and Design Lecture 28: Functors
2
Recap of Lecture 27 STL Algorithms Accumulate Reordering algorithms Searching algorithm Removing algorithm
3
Outline of Lecture 28 Adapters Predicate functions Function Objects
4
Iterator Adaptors 0123456 0000000 Source Target 0123456 0123456 Source Target Before copy After copy copy(start, stop, result)
5
Iterator Adaptors Iterator adaptors are defined in the header They are objects that act like iterators Don't actually point to elements of a container e.g. ostream_iterator
6
A closer look at ostream_iterator They are parameterized We passed it two pieces of information 1.A stream to write to, in this case cout 2.A separator string Demonstration (Iter_Adapt.cpp) ostream_iterator myItr(cout, " " ); vector ::iterator it;
7
ostream_iterator Iterator adaptors are iterators, and so they can be used in conjunction with the STL algorithms Supply an iterator adaptor instead to “trick” the algorithm Performing some complex task when it believes it's just writing values to a range copy(myVector.begin(), myVector.end(), ostream_iterator (cout, " "));
8
Common Iterator adapter NameExample back_insert_iterator back_insert_iterator > itr(myVector); front_insert_iterator front_insert_iterator > itr(myIntDeque); insert_iterator insert_iterator > itr( mySet, mySet.begin()); ostream_iterator ostream_iterator itr( myStream, "\n"); istream_iterator istream_iterator itr(cin);
9
A Word on Compatibility STL implementations are uniform C++ code that works on one compiler should work on any other compile Unfortunately, this is not the case string ConvertToLowerCase(string text) { transform (text.begin(), text.end(), text.begin(), tolower ); return text; }
10
Predicate functions Many of the STL generic algorithms take a functional argument template T accumulate (InputIterator first, InputIterator last, T init); template T accumulate( InputIt first, InputIt last, T init, BinaryOperation op ); Second version uses a given binary function The signature of the function should be equivalent to the following: Ret fun(const Type1 &a, const Type2 &b); Demonstration (AccumCustom.cpp, SortCustom.cpp)
11
Common binary predicates equal_to(){ if (arg1 == arg2) return TRUE; else return FALSE; } not_equal_to() greater() less() greater_equal() less_equal() logical_and() logical_or()
12
Common arithmetic functions plus(){ return ( arg1 + arg2); } minus(){ return ( arg1 - arg2 ); } multiplies(){ return ( arg1 * arg2 ); } divides(){ return ( arg1 / arg2 ); } modulus(){ return ( arg1 % arg2 ); }
13
Function Objects string mystr[] = {“Me”,”You”,”He”,”She”,”You”,”They”}; int mycount = count (mystr, myints+6, “You”); The function uses operator == to compare the individual elements to val. Count the number of string s that have length greater than 3 int numEvens = count_if(myVector.begin(), myVector.end(), LengthIsLessThanThree); bool LengthIsLessThanThree(const string& str) { return str.length() < 3; }
14
Alternative design A function has access the following information: Its local variables. Its parameters. Global variables. This is not an optimal solution It is error-prone, It is not scalable, It uses global variables bool LengthIsLessThanThree(const string& str, size_t length) { return str.length() < length; } Should be unary
15
Functors to rescue Issue: Unary function does not have access to enough information Intension: A unary function to act like a binary function without taking an extra parameter A functor (or function object) is an C++ class that acts like a function Can be accessed like objects, but behaves like functions MyClass myFunctor; cout << myFunctor(137) << endl;
16
More on functor signature Create an object that overloads the function call operator, operator () It is a function called operator (), not a function called operator that takes no parameters Defining a function that gets called if we invoke the object like a function cout << myFunctor(137) << endl; is equivalent to cout << myFunctor.operator()(137) << endl;
17
The operator() Return an object of any type (or even void) Can accept any number of parameters class MyFunctor { public: void operator() (const string& str) const { cout << str << endl; } };
18
Functions vs Functors A functor's function call operator is a member function A raw C++ function is a free function A function has access to Its local variables. Its parameters. Global variables. A functor has access to Its local variables. Its parameters. Global variables. Class data members.
19
count_if revisited int numEvens = count_if(myVector.begin(), myVector.end(), LengthIsLessThanThree); class ShorterThan { private: const size_t length; public: ShorterThan(size_t maxLength) : length(maxLength) {} bool operator() (const string& str) const { return str.length() < length; } }; count_if(myVector.begin(), myVector.end(), ShorterThan(length));
20
Function with local state Those local variables can be very useful Suppose you wanted to look at a container of XY coordinates and find the one closest to some point P. template ForwardIterator min_element (ForwardIterator first, ForwardIterator last, Compare comp );
21
Thank you Next Lecture: Functors
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.