1 / 28

Template Functions

Learn to implement template functions in C++ to find the maximum of different data types efficiently.

Download Presentation

Template Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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; }

  3. 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; }

  4. 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; }

  5. 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...

  6. 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; }

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

  8. 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; }

  9. 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; }

  10. 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; }

  11. 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; }

  12. C++ Standard Library • <algorithm> contains some template function • swap • max • min

  13. 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

  14. 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

  15. 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

  16. 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)

  17. 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 • * • ++

  18. 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();

  19. 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();

  20. * 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;

  21. ++ 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;

  22. The [ … )Pattern

  23. Pitfall • Do not access an iterator’s item after reaching end() • Remember end() is one location past the last item of the container

  24. 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);

  25. 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

  26. 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 ++) • == • !=

  27. Bag Template class with an Iterator • Bag iterator • begin • end • Declare an iterator for a bag • Bag<int>::iterator position;

  28. Summary • A template function depends on an underlying data type. • More complex template functions and template classes are discussed in Chapter 6.

More Related