More on the STL: Function Objects and Generic Algorithms in the STL - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

More on the STL: Function Objects and Generic Algorithms in the STL

Description:

Function Objects and Generic Algorithms in the STL. Fred Kuhns. Computer Science and Engineering ... find minimum element in container supporting ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 16
Provided by: Fre58
Category:

less

Transcript and Presenter's Notes

Title: More on the STL: Function Objects and Generic Algorithms in the STL


1
More on the STLFunction Objects and Generic
Algorithms in the STL
  • Fred Kuhns
  • Computer Science and Engineering
  • Applied Research laboratory
  • Washington University in St. Louis

2
Leverages Templates
// find minimum element in container
supporting // the operetor() and types T
supporting operators // operatorlt() and
operator() template ltclass Tgt const T min(const
T p, int sz) T minval p0 for (int i
1 i lt sz i) if (pi lt minval) minval
pi return minval
  • This is a simple, straight forward template
    function
  • But it is limited since it only supports the
    comparison operator lt defined for T
  • A better solution is to allow the comparison
    function to be passed as an argument.

3
Adding some flexibility
// find minimum element in container
supporting // the operetor() and types T
supporting operatorlt(). template ltclass T, bool
(LT)(const T, const T)gt const T min(const
Tp, int sz, LT lt) T minval p0 for
(int i 1 i lt sz i) if (lt(pi,
minval)) minval pi return minval
  • Increased flexibility by permitting a wider
    variety of comparison operations
  • But, now we have a function pointer adding
    overhead.

4
Function Objects
template ltclass Xgt class MyComp bool
operator()(X a, X b) // find minimum
element in container supporting // the
operetor() and types T supporting
operatorlt(). template ltclass T, class LTgt const
T min(const Tp, int sz, LT lt) T minval
p0 for (int i 1 i lt sz i) if
(lt(pi, minval)) minval pi return
minval
  • Now the comparison may be inlined and the
    function object may hold state

5
Another Example
// assumes arithimitic types which permit 0
assignment // and operator() template ltclass Tgt
Sum T sum_ public Sum(T i0) sum_(i)
void operator()(T x) sum_ x T
result() const return sum_ void
fn(listltdoublegt ld) Sumltdoublegt s s
for_each(ld.begin(), ld.end(), s) cout ltlt Sum
ltlt s.result() ltlt endl
6
From Whence do they Come?
  • Predefined set of arithmetic, relational and
    logical functors in the STL
  • Function Object bases, Predicates, Arithmetic
  • Predefined set of function adaptors
  • Binders, Adapters and Negators
  • Define your own

7
Predefined Function Objects
include ltfunctionalgt plusltintgt intAdd int i
10, j 20 int sum intAdd(i, j) // sort() by
default invokes the less-than operator vectorltstri
nggt names sort(names.begin(), names.end(),
greaterltstringgt())
creates tmp object (unnamed)
  • Each is a class template parameterized on the
    type of operands. See ltfunctionalgt
  • Primary use is as arguments to the generic
    functions

8
Predefined Function Objects
// predefined base classes for operators template
ltclass Arg, class Resgtstruct unary_function
typedef Arg argument_type typedef Res
result_type template ltclass Arg, class Arg2,
class Resgt struct binary_function typedef Arg
first_argument_type typedef Arg2
second_argument_type typedef Res
result_type // example, STL less
functor template ltclass Tgtstruct less public
binary_functionltT, T, boolgt bool
operator()(const Tx, const Ty) const return x
lt y
  • defined bases provide standard names for types
  • predicates function that returns a bool

9
Predicates
// example predicate template ltclass Tgtstruct
less public binary_functionltT, T, boolgt
bool operator()(const Tx, const Ty) const
return x lt y void fn(vectorltintgtvi,
listltintgt li) typedef listltintgtiterator
LI typedef vectorltintgtiterator VI pairltVI,
LIgt pl // find first pair of elements that do
not match pl mismatch(vi.begin(), vi.end(),
li.begin(), lessltintgt())
  • Compare element by element, in this case the
    elements of vi are compared to those of li. Finds
    the first element of vi that is _not_ less than
    the corresponding element of li.
  • What if I used lessltintgt rather than lessltintgt()
    in the call to missmatch()?

10
Define your own predicate
class Student string lname, fname, mname
int idnum class Student_eq public
unary_functionltStudent, bool) string
name public explicit Student_eq(const string
n) name(n) bool operator()(const
Students) const return s.lname
name void fn(listltStudentgt sl) typedef
listltStudentgtiterator SLI SLI p
find_if(sl.begin(), sl,end(), Student_eq(Smith))
  • here we defined a predicate that returns true if
    a student is found with the same last name

11
Adapters
  • Binder allows two parameter function object to
    be used with one
  • bind the first or second argument to objects
    data member. The remaining argument is taken from
    the call list.
  • I give examples in the following slides
  • Adapter member function and function pointer
    adapters
  • permits member function of an object to be called
    when using the generic algorithms
  • Negater negate result of predicate

12
Binders
template ltclass Tgt lt public unary_functionltT,
boolgt T arg2 public explicit lt(const T
x) arg2(x) bool operator()(const T x)
const return x lt arg2 void
fn(listltintgtc) listltintgtconst_iterator p
find_if(c.begin(),c.end(),ltltintgt(7))
  • Example were we explicitly create an object to
    hold the desired comparison value.
  • Occurs often enough the a generalized mechanism
    is provided in the STL

13
Binders
template ltclass BinOpgt class binder2nd public
unary_functionltBinOpfirst_argument_type,
BinOpresult_typegt protected BinOp op
typename BinOpsecond_argument_type
arg2 public binder2nd(const BinOp x, const
typename BinOpsecond_argument_typev)
op(x), arg2(v) result_type operator()(const
first_argument_type x) const return op(x,
arg2) templateltclass BinOp,class Tgt
binder2ndltBinOpgt bind2nd(const BinOpop, const
Tv) return binder2ndltBinOpgt(op, v) //
example use void fn(listltintgtc)
listltintgtconst_iterator p find_if(c.begin(),c.
end(),bind2nd(lessltintgt, 7))
parameterized for operand and operator types
return type
14
Simpler version
template ltclass Tgtstruct less_than public
binder2ndltlesslttgt, Tgt explicit
less_than(const T x) binder2nd(lessltTgt(), x)
void fn(listltintgt c) listltintgtconst_it
erator p find_if(c.begin(), c.end(),
less_thanltintgt(7))
15
Generic Algorithms
  • generally, first two arguments are a pair of
    iterators marking the first and last elements
  • Categories of algorithms
  • search
  • sorting/ordering
  • deletion/substitution
  • permutation
  • numeric
  • generation/mutation
  • relation
  • set
  • heap
Write a Comment
User Comments (0)
About PowerShow.com