Function Objects - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Function Objects

Description:

class Widget; struct WidgetTester : public. binary_function Widget,int, bool public: bool operator() (const Widget& wid,int testid) const { return wid.id() == testid; ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 13
Provided by: anniegro
Category:

less

Transcript and Presenter's Notes

Title: Function Objects


1
Function Objects
  • CS 341
  • Western Washington University

2
Whats a Function Object?
  • An instance of a class that defines the
    operator() as a member function
  • Example
  • class Increment
  • public
  • Increment(int base0) _value(base)
  • int operator() () return _value
  • private
  • int _value
  • vectorltintgt aVec(20)
  • generate(aVec.begin(), aVec.end(),
  • Increment())

3
Built-in Function Objects
include ltfunctionalgt Base Function
Objects template ltclass Arg1, class Resultgt
struct unary_function typedef Arg1
argument_type typedef Result result_type te
mplate ltclass Arg1, class Arg2, class
Resultgt struct binary_function typedef Arg1
first_argument_type typedef Arg2
second_argument_type typedef Result
result_type
4
Arithmetic template ltclass Tgt struct plus
public binary_functionltT,T,Tgt template ltclass
Tgt struct minus public binary_functionltT,T,Tgt
template ltclass Tgt struct times public
binary_functionltT,T,Tgt template ltclass Tgt
struct divides public binary_functionltT,T,Tgt te
mplate ltclass Tgt struct modulus public
binary_functionltT,T,Tgt template ltclass Tgt
struct negate public unary_functionltT,Tgt
5
Comparisons template ltclass Tgt struct equal_to
public binary_functionltT,T,boolgt template
ltclass Tgt struct not_equal_to public
binary_functionltT,T,boolgt template ltclass Tgt
struct greater public binary_functionltT,T,boolgt
template ltclass Tgt struct less public
binary_functionltT,T,boolgt template ltclass Tgt
struct greater_equal public binary_functionltT,T
,boolgt template ltclass Tgt struct less_equal
public binary_functionltT,T,boolgt
6
Logical Operations template ltclass Tgt struct
logical_and public binary_functionltT,T,boolgt t
emplate ltclass Tgt struct logical_or public
binary_functionltT,T,boolgt template ltclass Tgt
struct logical_not public unary_functionltT,T,boo
lgt Negators template ltclass Predicategt struct
unary_negate public unary_functionlttypename
Predicateargument_type, boolgt template
ltclass Predicategt struct binary_negate public
binary_functionlttypename Predicatefirst_argumen
t_type, typename Predicatesecond_argument_type,
boolgt
7
Binders template ltclass Operationgt struct
binder1st template ltclass Operationgt struct
binder2nd Adaptors template ltclass Arg, class
Resultgt class pointer_to_unary_function templat
e ltclass Arg1, class Arg2, class Resultgt class
pointer_to_binary_function
8
Why use function objects?
  • Theyre required arguments to many STL algorithms
  • To employ an existing function object provided by
    the standard library instead of a new function
  • To improve execution by using an inline function
    call
  • To allow a function object to access or set state
    information that is held by an object.

9
Negator Example
class Widget struct WidgetTester public
binary_functionltWidget,int, boolgt public bool
operator() (const Widget wid,int testid)
const return wid.id() testid //
Negator Example // The following returns a binary
// predicate which takes exactly the // same
arguments as the WidgetTester() // and is true
when the WidgetTester() // is false, and false
otherwise not2(WidgetTester())
10
Plus Example
template ltclass Tgt struct plus
public binary_functionltT, T, Tgt public T
operator() (const T arg1, const T arg2)
return arg1 arg2 vectorltintgt aVec,
aVec2, result // Code to initialize aVec and
aVec2 // The following populates the result //
with the memberwise sum of // aVec and
aVec2 transform(aVec.begin(), aVec.end(), aVec2.be
gin(), result.begin(), plusltintgt())
11
A BiggerThan Class
template ltclass Tgtclass BiggerThan public
unary_functionltT,boolgt public BiggerThan(const
T val)_value(val) bool operator() (const T
val) return val gt _value private const T
_value vectorltintgt aVec vectorltintgtitera
tor firstBig find_if(aVec.begin(), aVec.end(),
BiggerThanltintgt(100))
12
Binders
  • A function adaptor used to build new function
    objects
  • Takes a binary(2-argument) function, and binds
    either the 1st or 2nd argument to a specific
    value
  • Yields a one argument function

vectorltintgtiterator firstBig
find_if(aVec.begin(), aVec.end(),
bind2nd(greaterltintgt(),100))
Write a Comment
User Comments (0)
About PowerShow.com