150 likes | 278 Vues
Advanced Programming. Introduction to C++ Templates. Case-Study. What we have: A function that retrieves the minimum float in an array of floats
E N D
Advanced Programming Introduction to C++ Templates
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.
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; }
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; }
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; }
Supporting Arbitrary Criterion (Cont.) Comparison can be: • A function • An object implementing () operator
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);
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>);
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));
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 ???
Supporting Arbitrary List (Cont.) int array[50]; int* minPtr = findMin(array, array+50, lessThan<int>); min = *minPtr;
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 !
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)
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; }
Supporting Arbitrary List (Cont.) ListIterator b(list); ListIterator e(null); ListIteratorminIt = findMin(b, e, lessThan<int>); Int min = *minIt;