1 / 15

Advanced Programming

Advanced Programming. Introduction to C++ Templates. Case-Study. What we have: A function that retrieves the minimum float in an array of floats

yuki
Download Presentation

Advanced Programming

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 Introduction to C++ Templates

  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 a traversable list of arbitrary type according to arbitrary comparison criterion.

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

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

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

  6. Supporting Arbitrary Criterion (Cont.) Comparison can be: • A function • An object implementing () operator

  7. Supporting Arbitrary Criterion (Cont.) Example using a function: boollessAlpha(const Student &s1, const Student &s2) {return s1.mName < s2.mName;} Student array[10]; intfirstInList = findMin(array, 10, lessAlpha);

  8. 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>);

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

  10. 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 ???

  11. Supporting Arbitrary List (Cont.) int array[50]; int* minPtr = findMin(array, array+50, lessThan<int>); min = *minPtr;

  12. Supporting Arbitrary List (Cont.) We can further rewrite the function as follows template<class T, class Comparison> TfindMin(T begin, 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 !

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

  14. Supporting Arbitrary List (Cont.) structListIterator { Listiterator(Node *node) : mCurrent(node) {} ListIterator &operator++() { mCurrent = mCurrent->mNext; return *this; } bool operator!=(const ListIterator &it) const {return mCurrent != it->mCurrent;} const int & operator *() {return mCurrent->mData;} Node *mCurrent; }

  15. Supporting Arbitrary List (Cont.) ListIterator b(list); ListIterator e(null); ListIteratorminIt = findMin(b, e, lessThan<int>); Int min = *minIt;

More Related