C++_function_objects - Overview of STL Function Objects...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++   STL function objects Overview of STL Function Objects Function Object (also known as a  Functor ) STL Function Objects  support  function call syntax E.g., a class that declares and defines  operator() E.g., by being a function pointer GoF Command pattern  may  be applied to classes and  structs (user-defined types) Encapsulate a function call as an object Some or all of these can be member variables Target object reference, function to invoke, arguments Generic programming allows diverse types A function pointer (example from Lippman, LaJoie, Moo) An instance of a class providing  operator()
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++   STL function objects Function Objects Extend STL Algorithms Make the algorithms even more general Can be used parameterize  policy E.g. , the order produced by a sorting algorithm Each functor does a single, specific operation Often implemented as small classes or structs Often has only one public member function,  operator()  Functors may also have member variables Arguments not stored may be supplied at point of call Member variables can parameterize the operation E.g. , the value k for a +k functor E.g. , arguments for an invocation on a remote object
Background image of page 2
CSE 332: C++   STL function objects Examples of Function Object Usage struct GT_magnitude : public binary_function<double, double, bool> { bool operator() (double x, double y) { return fabs(y) < fabs(x); } }; struct LT_magnitude : public binary_function<double, double, bool> { bool operator() (double x, double y) { return fabs(x) < fabs(y); } }; int main (int, char **) { vector<double> u,v; for (double d = 0.0; d < 10.1; d += 1.0){ u.push_back (d); v.push_back (d); } sort (u.begin(), u.end(), GT_magnitude()); sort (v.begin(), v.end(), LT_magnitude()); ostream_iterator<double> o (cout, “ ”)); copy (u.begin(), u.end(), o); copy (v.begin(), v.end(), o); return 0; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++   STL function objects Function Object Concepts Basic Function Object Concepts Generator Unary Function Binary Function Adaptable Function Objects (turn functions into functors) Adaptable Generator Adaptable Unary Function Adaptable Binary Function Predicates (return a boolean result) Predicate Binary Predicate Adaptable Predicate Adaptable Binary Predicate Strict Weak Ordering Specialized Concepts Random Number Generator Hash Function
Background image of page 4
CSE 332: C++   STL function objects Function Object Concept Hierarchy Adaptable Function Object Basic Function Object Specialized Predicate i s - r e f n d b y Generator Unary Function
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/07/2008 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 23

C++_function_objects - Overview of STL Function Objects...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online