280 likes | 298 Views
Learn to implement template functions in C++ to find the maximum of different data types efficiently.
E N D
Template Functions • Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. • This presentation shows how to implement and use the simplest kinds of templates: template functions. CHAPTER 6 Data Structures and Other Objects
Finding the Maximum of Two Integers • Here’s a small function that you might write to find the maximum of two integers. int maximum(int a, int b) { if (a > b) return a; else return b; }
Finding the Maximum of Two Doubles • Here’s a small function that you might write to find the maximum of two double numbers. int maximum(double a, double b) { if (a > b) return a; else return b; }
Finding the Maximum of Two Knafns • Here’s a small function that you might write to find the maximum of two knafns. int maximum(knafn a, knafn b) { if (a > b) return a; else return b; }
One Hundred Million Functions... int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Hoo a, Hoo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Doo a, Doo b) { if (a > b) return a; else return b; } int maximum(Moo a, Moo b) { if (a > b) return a; else return b; } int maximum(Noo a, Noo b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Foo a, Foo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Poo a, Poo b) { if (a > b) return a; else return b; } int maximum(Boo a, Boo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Koo a, Koo b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Joo a, Joo b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Ioo a, Ioo b) { if (a > b) return a; else return b; } int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } int maximum(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo b) { if (a > b) return a; else return b; } int maximum(Coo a, Coo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } int maximum(Goo a, Goo b) { if (a > b) return a; else return b; } int maximum(Loo a, Loo b) { if (a > b) return a; else return b; } • Suppose your program uses 100,000,000 different data types, and you need a maximum function for each...
A Template Function for Maximum • This template function can be used with many data types. template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }
A Template Function for Maximum • When you write a template function, you choose a data type for the function to depend upon... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }
A Template Function for Maximum • A template prefix is also needed immediately before the function’s implementation: template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }
Using a Template Function cout << maximum(1,2); cout << maximum(1.3, 0.9); ... • Once a template function is defined, it may be used with any adequate data type in your program... template <class Item> Item maximum(Item a, Item b) { if (a > b) return a; else return b; }
Finding the Maximum Item in an Array • Here’s another function that can be made more general by changing it to a template function: int array_max(int data[ ], size_t n) { size_t i; int answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; }
Finding the Maximum Item in an Array • Here’s the template function: template <class Item> Item array_max(Item data[ ], size_t n) { size_t i; Item answer; assert(n > 0); answer = data[0]; for (i = 1; i < n; i++) if (data[i] > answer) answer = data[i]; return answer; }
C++ Standard Library • <algorithm> contains some template function • swap • max • min
Parameter Matching for Templates • Template parameter must appear in parameter list of template function • Template instantiation – compiler selects data type so that have an exact match with the type of the formal arguments
Parameter Matching for Templates template<class Item> size_t index_of_maximal(const Item data[], size_t n); const size_t SIZE = 5; double data[SIZE]; …… cout << index_of_maximal(data, SIZE); //won’t work with many compilers cout << index_of_maximal(data, 5); //won’t work with many compilers template<class Item, class size_type> size_t index_of_maximal(const Item data[], size_type n); size type is const size_t size type is int
Template classes • Change the template class definition, the template header • Implement functions for the template class • Inside the class definition, template parameters can be used freely • Outside of the class definition, a template header is needed for each function • Make the implementation visible: the header file must include the implementation file • As a result, the implementation file is not linked in as would a regular implementation file • Do Not Place Using Directives in a Template Implementation
Template Classes • Example • bag operator + (const bag& b1, const bag& b2) • template<class Item> • bag<Item> operator + (const bag<Item>& b1, const bag<Item> & b2)
Review: Iterators • An iterator is an object that permits a programmer to easily step through all the items in a container, examining the items changing them. • Member functions: • begin • end • * • ++
begin member function • Its return value is an iterator that provides access to the first item in the container multiset<string> actors; multiset<string>::iterator role; role = actors.begin();
end member function • Its return value is an iterator that provides access to the last item in the container multiset<string> actors; multiset<string>::iterator role; role = actors.end();
* operator • The * operator can be used to access (cannot change) the current element of the iterator multiset<string> actors; multiset<string>::iterator role; role = actors.begin(); cout<< *role <<endl;
++ operator • ++ operator can be used to move an iterator forward to the item in its collection multiset<string> actors; multiset<string>::iterator role; role = actors.begin(); ++role;
Pitfall • Do not access an iterator’s item after reaching end() • Remember end() is one location past the last item of the container
Other multiset operations • != • == • It is an error to compare two iterators from different containers • find • erase multiset<int> m; multiset<int>::iterator position; position = m.find(42); if(position != m.end()) m.erase(position);
STL algorithms • STL contains a group of functions in the <algorithms> to manipulate container classes • copy • set_union: create a union of two sets iterator set_union (iterator1 first1, iterator1 last1, iterator2 first2, iterator2 last2, iterator3 result); • resultis an output iterator: it can be used to change values in the collection, not just fetch them
Node Template Class • Node Iterator • The node_iterator is derived from std::iterator • Constructor node_iterator<int> start(head_ptr); node_iterator<int> finish; node_iterator<int> position; for(position = start; position != finish; ++position) { if ((*position % 2) == 1) *postion = 0; } • Operations: • * • ++ (prefix ++ is more efficient than postfix ++) • == • !=
Bag Template class with an Iterator • Bag iterator • begin • end • Declare an iterator for a bag • Bag<int>::iterator position;
Summary • A template function depends on an underlying data type. • More complex template functions and template classes are discussed in Chapter 6.