 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
This lecture introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation shows how.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC212 Data Structure - Section FG Lecture 11 Templates, Iterators and STL Instructor: Professor Zhigang Zhu Department of Computer Science City College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 CSC212 Data Structure Lecture 4 Container Classes Instructor: George Wolberg Department of Computer Science City College of New York.
Xiaoyan Li CSC211 Data Structures Lecture 2 ADT and C++ Classes (I) Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Templates, Iterators and STL
Using a Stack Chapter 6 introduces the stack data type.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Using a Stack Chapter 6 introduces the stack data type.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Using a Stack Chapter 6 introduces the stack data type.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Really reusable software
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

 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. Template Functions CHAPTER 6 Data Structures and Other Objects

 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 Integers

 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 Doubles

 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; } Finding the Maximum of Two Knafns

 Suppose your program uses 100,000,000 different data types, and you need a maximum function for each... int maximum(Knafn a, Knafn b) { if (a > b) return a; else return b; } One Hundred Million Functions... 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(Noo a, Noo 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(Loo a, Loo 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(Hoo a, Hoo 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(Doo a, Doo 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(Boo a, Boo 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(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(Noo a, Noo 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(Loo a, Loo 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(Hoo a, Hoo 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(Doo a, Doo 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(Boo a, Boo 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(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(Noo a, Noo 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(Loo a, Loo 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(Hoo a, Hoo 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(Doo a, Doo 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(Boo a, Boo b) { if (a > b) return a; else return b; }

 This template function can be used with many data types. template 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 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 Item maximum(Item a, Item b) { if (a > b) return a; else return b; } A Template Function for Maximum

 Once a template function is defined, it may be used with any adequate data type in your program... template 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);...

 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 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; } Finding the Maximum Item in an Array

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

T HE E ND Presentation copyright 2004, Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.