C++_STL_functors - STL Functors: Functions vs. Function...

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

View Full Document Right Arrow Icon
CSE 332: C++ STL functors STL Functors: Functions vs. Function Objects STL Functors support function call syntax Algorithms invoke functions and operators E.g., calling operator< during a sort algorithm Those expressions define type requirements In many cases, can plug in alternative behaviors The STL allows diverse functor types A function pointer (from Lippman, LaJoie, Moo) bool (*PF) (const string &, const string &) // function ptr type bool *pf (const string &, const string &) // function named pf Instance of a class/struct providing operator() Either one can be invoked (called) But may have to deal with signature (and return type)
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 functors STL Functors Extend STL Algorithms Make the algorithms even more general Can be used parameterize policy E.g. , the order produced by a sorting algorithm E.g. , the order maintained by an associative container Each functor does a single, specific operation Often implemented as small functions or classes/structs E.g., a struct with one public member function, operator() Function objects 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 functor that adds k to another value E.g. , arguments for an invocation on a remote object
Background image of page 2
CSE 332: C++ STL functors Function Object Use in an Algorithm 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 functors Function Use in an Algorithm #include <iostream> #include <vector> #include <string> #include <iterator> #include <algorithm> using namespace std; struct Employee { Employee (const char * n, int i) : name_(n), id_(i) {} string name_; int id_; }; int main (int, char *[]) { vector<EmployeePtr> v; v.push_back(new Employee("Claire", 23451)); v.push_back(new Employee("Bob", 12345)); cout << "v: " ; copy (v.begin(), v.end(), ostream_iterator<EmployeePtr>(cout)); cout << endl; // "v: Claire 23451 Bob 12345 Alice 54321 " sort (v.begin(), v.end(), id_compare); function name ok here heap object
Background image of page 4
CSE 332: C++ STL functors Function Object Use in a Container #include <set> #include <string> #include <iterator> #include <algorithm> using namespace std; struct Employee { Employee (const char * n, int i) : name_(n), id_(i) {} string name_; int id_; }; ostream& operator<< (ostream & os, const Employee & e) { int main (int, char *[]) { vector<Employee> v; v.push_back(Employee("Claire", 23451)); v.push_back(Employee("Bob", 12345)); v.push_back(Employee("Alice", 54321)); cout << "v: " ; copy (v.begin(), v.end(), // "v: Claire 23451 Bob 12345 Alice 54321 " set<Employee> s; s.insert(v.begin(), v.end()); function object needed temporary object
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 03/03/2010 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 24

C++_STL_functors - STL Functors: Functions vs. Function...

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