1 / 14

Advanced Programming Techniques Spring 2010

Advanced Programming Techniques Spring 2010. C ++ Templates Case Study. Case-Study. What we have : A function that retrieves the minimum float in an array of floats What we want : A function that retrieves the minimum element in an arbitrary traversable list of arbitrary type

trapper
Download Presentation

Advanced Programming Techniques Spring 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Programming TechniquesSpring 2010 C++ Templates Case Study

  2. Case-Study • What we have: A function that retrieves the minimum float in an array of floats • What we want: A function that retrieves the minimum element • in an arbitrary traversable list of arbitrary type • according to arbitrary comparison criterion.

  3. Case-Study (Cont.) intfindMin(float arr[], int n) { int min = 0; for(inti = 0; i < n; i++) { if(arr[i] < arr[min]) {min = i;} } return min; }

  4. 1-Supporting Arbitrary Type template<class T> intfindMin(Tarr[], int n) { int min = 0; for(inti = 0; i < n; i++) { if(arr[i] < arr[min]) {min = i;} } return min; }

  5. 2-Supporting Arbitrary Criterion template<class T, class Comparison> intfindMin(Tarr[], int n, Comparison c) { int min = 0; for(inti = 0; i < n; i++) { if(c(arr[i], arr[min])) {min = i;} } return min; }

  6. 2-Supporting Arbitrary Criterion (cont.) Comparison can be done using: • A function • An object implementing () operator:“Functor”

  7. Supporting Arbitrary Criterion (Cont.) Example using a function: template<class T> boollessThan(const T &s1, const T &s2) {return s1 < s2;} int[10]; intminIdx = findMin(array, 10, lessThan<int>);

  8. 2-Supporting Arbitrary Condition(Cont.) Example using a Functor: class CourseComp { public: CourseComp(intcourseKey): mKey(courseKey) {} bool operator()(const Student &s1, const Student &s2){return s1.getGrade(mKey) < s2.getGrade(mKey);} private: intmKey; } Studentarray[10]; Int lowestIn450 = findMin(array, 10, CourseComp(450));

  9. Supporting Arbitrary List Let’s first rewrite the function as follows template<class T, class Comparison> T*findMin(T* begin, T* end, Comparison c) { T* min = begin; for(T* it = begin; it != end; ++it) { if(c(*it, *min)) {min = it;} } return min; } How can it be invoked ??? Section 1-STL Advanced Programming

  10. Supporting Arbitrary List (Cont.) To invoke findMin on an array: intarray[50]; int* minPtr = findMin(array, array+50, lessThan<int>); min = *minPtr;

  11. Supporting Arbitrary List (Cont.) We can further rewrite the function as follows template<class T, class Comparison> TfindMin(Tbegin, T end, Comparison c) { T min = begin; for(T it = begin; it != end; ++it) { if(c(*it, *min)) {min = it;} } return min; } And it should still work !

  12. Supporting Arbitrary List (Cont.) • Suppose we have a linked list struct Node { intmData; Node *mNext; } Node *list; How can we use findMinto find the minimum element in the linked list ??? (No code changes)

  13. Supporting Arbitrary List (Cont.) Let’s create our own iterator: structListIterator { Listiterator(Node *node) : mCurrent(node) {} ListIterator &operator++() { mCurrent = mCurrent->mNext; return *this; } const int & operator *() {return mCurrent->mData;} Node *mCurrent; }

  14. Supporting Arbitrary List (Cont.) To invoke findMin for the linked list: ListIterator b(list); ListIterator e(null); ListIteratorminIt = findMin(b, e, lessThan<int>); Int min = *minIt;

More Related