140 likes | 280 Vues
This case study explores advanced C++ programming techniques utilizing templates to create a function capable of finding the minimum element in various traversable lists of arbitrary types. Traditional methods limit the function to specific data types, while our approach supports generic types and arbitrary comparison criteria. We demonstrate how to implement this using templates, including a custom functor for comparison, and show how to adapt the function for different containers such as arrays and linked lists. Learn effective template programming to enhance code reusability.
E N D
Advanced Programming TechniquesSpring 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 • according to arbitrary comparison criterion.
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; }
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; }
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; }
2-Supporting Arbitrary Criterion (cont.) Comparison can be done using: • A function • An object implementing () operator:“Functor”
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>);
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));
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
Supporting Arbitrary List (Cont.) To invoke findMin on an array: intarray[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(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 !
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)
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; }
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;