Presentation is loading. Please wait.

Presentation is loading. Please wait.

Washington WASHINGTON UNIVERSITY IN ST LOUIS More on the STL: Function Objects and Generic Algorithms in the STL Fred Kuhns Computer Science and Engineering.

Similar presentations


Presentation on theme: "Washington WASHINGTON UNIVERSITY IN ST LOUIS More on the STL: Function Objects and Generic Algorithms in the STL Fred Kuhns Computer Science and Engineering."— Presentation transcript:

1 Washington WASHINGTON UNIVERSITY IN ST LOUIS More on the STL: Function Objects and Generic Algorithms in the STL Fred Kuhns Computer Science and Engineering Applied Research laboratory Washington University in St. Louis

2 2 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Leverages Templates This is a simple, straight forward template function But it is limited since it only supports the comparison operator ‘ < ‘ defined for T A better solution is to allow the comparison function to be passed as an argument. // find minimum element in container supporting // the operetor[]() and types T supporting operators // operator<() and operator=()‏ template const T& min(const T* p, int sz)‏ { T* minval = &p[0]; for (int i = 1; i < sz; ++i)‏ if (p[i] < *minval) minval = &p[i]; return *minval; }

3 3 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Adding some flexibility Increased flexibility by permitting a wider variety of comparison operations But, now we have a function pointer adding overhead. // find minimum element in container supporting // the operetor[]() and types T supporting operator<(). template const T& min(const T*p, int sz, LT lt)‏ { T *minval = &p[0]; for (int i = 1; i < sz; ++i)‏ if (lt(p[i], *minval)) minval = &p[i]; return *minval; }

4 4 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Function Objects Now the comparison may be inlined and the function object may hold state template class MyComp { … bool operator()(X& a, X& b) {…} … } // find minimum element in container supporting // the operetor[]() and types T supporting operator<(). template const T& min(const T*p, int sz, LT lt)‏ { T* minval = &p[0]; for (int i = 1; i < sz; ++i)‏ if (lt(p[i], *minval)) minval = &p[i]; return *minval; }

5 5 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Another Example // assumes arithmetic types which permit 0 assignment // and operator+=()‏ template Sum { T sum_; public: Sum(T i=0) : sum_(i) {} void operator()(T x) {sum_ += x;} T result() const {return sum_;} }; void fn(list & ld)‏ { Sum sumObj; s = for_each(ld.begin(), ld.end(), sumObj); cout << “Sum = ” << sumObj.result() << endl; }

6 6 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Where do you find functors? Predefined set of arithmetic, relational and logical functors in the STL Function Object bases, Predicates, Arithmetic 1.Predefined set of function adaptors: Binders, Adapters and Negators Define your own

7 7 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Predefined Function Objects Each is a class template parameterized on the type of operands. See Primary use is as arguments to the generic functions #include plus intAdd; int i = 10, j = 20; int sum = intAdd(i, j); // sort() by default invokes the less-than operator vector names; … sort(names.begin(), names.end(), greater ()); creates tmp object (unnamed) ‏

8 8 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Predefined Function Objects defined bases provide standard names for types predicates: function that returns a bool // predefined base classes for operators template struct unary_function { typedef Arg argument_type; typedef Res result_type; }; template struct binary_function { typedef Arg first_argument_type; typedef Arg2 second_argument_type; typedef Res result_type; }; // example, STL less functor template struct less : public binary_function { bool operator()(const T&x, const T&y) const {return x < y;} };

9 9 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Predicates 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 less rather than less () in the call to missmatch() ? // example predicate template struct less : public binary_function { bool operator()(const T&x, const T&y) const {return x < y;} }; void fn(vector &vi, list & li)‏ { typedef list ::iterator LI; typedef vector ::iterator VI; pair pl; // find first pair of elements that do not match pl = mismatch(vi.begin(), vi.end(), li.begin(), less ()); … }

10 10 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Define your own predicate here we defined a predicate that returns true if a student is found with the same last name class Student { string lname, fname, mname; int idnum; … }; class Student_eq : public unary_function <Student, bool) { string name; public: explicit Student_eq(const string& n) : name(n) {} bool operator()(const Student&s) const {return s.lname == name;} }; void fn(list & sl)‏ { typedef list ::iterator SLI; SLI p = find_if(sl.begin(), sl,end(), Student_eq(“Smith”)); … }

11 11 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Adapters Binder: allows two parameter function objects to be used with one –bind the first or second argument to object’s 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 12 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Binders Example where we explicitly create an object to hold the desired comparison value. Occurs often enough the a generalized mechanism is provided in the STL template lt : public unary_function { T arg2; public: explicit lt(const T& x) : arg2(x) {} bool operator()(const T& x) const {return x < arg2;} }; void fn(list &c)‏ { list ::const_iterator p = find_if(c.begin(),c.end(), lt (7) ); … }

13 13 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Binders template class binder2nd : public unary_function { protected: BinOp op; typename BinOp::second_argument_type arg2; public: binder2nd(const BinOp& x, const typename BinOp::second_argument_type&v)‏ : op(x), arg2(v) {} result_type operator()(const first_argument_type& x) const {return op(x, arg2);} }; template binder2nd bind2nd(const BinOp&op, const T&v)‏ { return binder2nd (op, v); } // example use void fn(list &c)‏ { list ::const_iterator p = find_if(c.begin(),c.end(),bind2nd(less, 7)); … } return type parameterized for operand and operator types

14 14 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts Simpler version template struct less_than : public binder2nd, T> { explicit less_than(const T& x) : binder2nd(less (), x) {} }; void fn(list & c)‏ { list ::const_iterator p = find_if(c.begin(), c.end(), less_than (7)); }

15 15 Fred Kuhns (12/02/08) ‏ CS422 – Operating Systems Concepts 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


Download ppt "Washington WASHINGTON UNIVERSITY IN ST LOUIS More on the STL: Function Objects and Generic Algorithms in the STL Fred Kuhns Computer Science and Engineering."

Similar presentations


Ads by Google