Template Functions Chapter 6 introduces templates which are













![Parameter Matching for Templates template<class Item> size_t index_of_maximal(const Item data[], size_t n); const size_t Parameter Matching for Templates template<class Item> size_t index_of_maximal(const Item data[], size_t n); const size_t](https://slidetodoc.com/presentation_image/55a01b4ab52baad878b3c680d7c95716/image-14.jpg)














- Slides: 28

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. . . Suppose your program uses 100, 000 different data types, and you need a maximum function for each. . . int maximum(Hoo a, Hoo b) int maximum(Hoo { maximum(Doo a, Doo b) a, Hoo b) int maximum(Moo a, int Moo b) int maximum(Noo a, Noo b)if (a > b) { maximum(Doo a, Doo b) int maximum(Hoo a, Hoo b) { int maximum(Moo a, int Moo b) { int maximum(Noo a, Noo b)if (a > b) { { return a; b) { if (a > b) int maximum(Doo a, Doo { if (a > b) int maximum(Moo a, Moo b) { if (a > b) { return a; int maximum(Noo a, Noo b)if (a > b) else return a; if (a > b) return a; { if (a > b) return a; if (a > b) else { return a; return b; return a; else int maximum(Foo a, Foo b) return a; else if (a > b)else return a; return b; if (a > b) else } int maximum(Foo a, Foo b) else return b; { return a; else return b; return a; return b; else } return a; return b; { return b; } if (a > b) else int maximum(Foo a, Foo b) return b; } else } if (a > b) } return a; return b; { } return b; return a; else } if (a > b) } } else return b; return a; int maximum(Poo a, Poo b) return b; } else int maximum(Boo a, b) Boo b) int maximum(Poo a, Poo { } int maximum(Boo a, Boo b) return b; { { int maximum(Koo a, Koo b) if (a > b) int maximum(Poo a, Poo b) { } int maximum(Boo a, { Boo b) int maximum(Koo a, Koo b) if (a if> (a b) > b) return a; { if (a > b) int maximum(Joo a, Joo b) return { { return a; a; if (a > b) else int maximum(Koo a, Koo b) if (a > b) int maximum(Joo a, Joo b) return a; if (a > b) a, Knafn {b)return if (a > b) int maximum(Ioo a, Ioo b) else int maximum(Knafn a; return b; { return a; else if (a > b) int maximum(Joo a, Joo b) return b; a, Knafn {b)return a; int maximum(Ioo a, Ioo b) int maximum(Knafn a; { return b; { else } if (a > b) else if (a > b) return b; return a; { {} } else { if (a >else b) int maximum(Ioo a, Ioo b) return ifb; (a > b) int maximum(Knafn a, Knafn b)return a; return b; return a; } else if (a > b) return b; return a; { return a; } {} else return b; return a; } else} if (a > b) return ifb; (a > b) return b; } else return b; return a; } } int maximum(Coo a, Coo b) return b; } else int maximum(Coo a, Coo b) { } } } return b; { if (a > b) int maximum(Goo a, Goo b) int maximum(Coo a, Coo b) int maximum(Loo a, Loo b) } } if (a > b) int maximum(Goo a, Goo b) return a; { { int maximum(Loo a, Loo b) { return a; { else if (a > b) { if (a > b) int maximum(Goo a, Goo b) if (a > b) int maximum(Loo a, Loo b) else if (a > b) return b; return a; if (a > b) return a; { return b; return a; } else if (a > b) else return a; if (a > b) } else return b; return a; return b; } } else return b; } else } return b; } }

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 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; } cout << maximum(1, 2); cout << maximum(1. 3, 0. 9); . . .

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 templateclass Item sizet indexofmaximalconst Item data sizet n const sizet Parameter Matching for Templates template<class Item> size_t index_of_maximal(const Item data[], size_t n); const size_t](https://slidetodoc.com/presentation_image/55a01b4ab52baad878b3c680d7c95716/image-14.jpg)
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 template<class Item, class size_type> size_t index_of_maximal(const Item data[], size_type n);

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& b 1, const bag& b 2) template<class Item> bag<Item> operator + (const bag<Item>& b 1, const bag<Item> & b 2)

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 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;

The [ … )Pattern

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 (iterator 1 first 1, iterator 1 last 1, iterator 2 first 2, iterator 2 last 2, iterator 3 result); result is 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.