Upload
yuki
View
16
Download
2
Embed Size (px)
DESCRIPTION
Advanced Programming. Introduction to C++ Templates. Case-Study. What we have: A function that retrieves the minimum float in an array of floats - PowerPoint PPT Presentation
Citation preview
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.)
int findMin(float arr[], int n){
int min = 0;
for(int i = 1; i < n; i++){
if(arr[i] < arr[min]) {min = i;}}
return min;}
Supporting Arbitrary Typetemplate<class T>int findMin(T arr[], int n){
int min = 0;
for(int i = 1; i < n; i++){
if(arr[i] < arr[min]) {min = i;}}
return min;}
Supporting Arbitrary Criteriontemplate<class T, class Comparison>int findMin(T arr[], int n, Comparison c){
int min = 0;
for(int i = 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:bool lessAlpha(const Student &s1, const Student
&s2) {return s1.mName < s2.mName;}Student array[10];int firstInList = findMin(array, 10, lessAlpha);
Supporting Arbitrary Criterion (Cont.)
Example using a function:template<class T>bool lessThan(const T &s1, const T &s2) {return
s1 < s2;}int[10];int minIdx = findMin(array, 10, lessThan<int>);
Supporting Arbitrary Condition (Cont.)
Example using an object:class CourseComp{
public: CourseComp(int courseKey) : mKey(courseKey) {}bool operator()(const Student &s1, const Student &s2) {return s1.getGrade(mKey) < s2.getGrade(mKey);}private: int mKey;
}
int array[10];Int lowestIn450 = findMin(array, 10, CourseComp(450));
Supporting Arbitrary ListLet’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>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;}
And it should still work !
Supporting Arbitrary List (Cont.)
• Suppose we have a linked liststruct Node{
int mData;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.)struct ListIterator{
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);ListIterator minIt = findMin(b, e, lessThan<int>);Int min = *minIt;