Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes.

Slides:



Advertisements
Similar presentations
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Vectors, lists and queues
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
EC-241 Object-Oriented Programming
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Starting Out with C++, 3 rd Edition Chapter 18 – Stacks and Queues.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
C++ STL CSCI 3110.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Section 3.5, class notes Iterators Generic algorithms.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.
This lecture introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation shows how.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CSC212 Data Structure - Section FG Lecture 11 Templates, Iterators and STL Instructor: Professor Zhigang Zhu Department of Computer Science City College.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ Queues.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 14: Overloading and Templates Overloading will not be covered.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Data Abstraction: The Walls
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Template Classes and Functions
Templates, Iterators and STL
Doubly Linked List Review - We are writing this code
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Chapter 19: Stacks and Queues.
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 11 Generic Collections
Introduction to Programming
CSC 143 Stacks [Chapter 6].
Template Functions Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes. This presentation.
Pointers & Dynamic Data Structures
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Really reusable software
Standard Template Library
Standard Template Library
Presentation transcript:

Templates 1. Template Functions 2. Template Class 3. Bag Class Template 4. Standard Library Template (STL) Classes Standard Library Template (STL) Classes Standard Library Template (STL) Classes 5. Vector Class

 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; } Function Template

 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

 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[ ], int n) { int i; int answer; 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[ ], int n) { int 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

Bag Class Template Header #ifndef BAG_H #define BAG_H #define MAXIMUM_SIZE 100 template class bag { public: bag(); bag(int max); int getCount(); bool isFull(); bool isEmpty(); void remove (T item); void clear(); void display();... private: T *data; int count; int capacity; }; #include bag.template // Note #endif

Bag Class Template Implementation #include #include "bag.h" using namespace std; template bag ::bag(){ capacity = MAXIMUM_SIZE; data = new T [capacity; count = 0; } template bag ::bag(int max){ capacity = max; data = new T [capacity]; count = 0; }... template int bag ::getCount(){ return count; } template bool bag ::isFull(){ return count == capacity; } template bool bag ::isEmpty(){ return count == 0; }...

Client Program for Bag Template #include #include "bag.h" using namespace std; int main() { // Check with int bag iBag; int iList[] = {2, 4, 6}; int iCount = 3; for (int i = 0; i < iCount; i++){ iBag.insert(iList[i]); } cout << "Int bag contents: "; iBag.display(); cout << endl; iBag.remove(iList[0]); cout << "After removing: “; iBag.display(); cout << endl;... // check with string bag sBag; string sList[] = {"Anne", "Bill", "Cathy"}; int sCount = 3; for (int i = 0; i < sCount; i++){ sBag.insert(sList[i]); } cout << "String contents: "; sBag.display(); cout << endl; sBag.remove(sList[0]); cout << "After removing: “; sBag.display(); cout << endl; system("PAUSE"); return EXIT_SUCCESS; }

Some STL Class Templates vector Array list Doubly-linked list slist Singly-linked list queue FIFO (first-in, first-out) structure deque Array-like structure, with efficient insertion and removal at both ends set Set of unique elements stack LIFO (last in, first out) structure More templates

Your Turn  Among the member functions in the vector class template provided by STL are:  vector()—constructor  void push_back(T item)—insert at back  T operator[](int i)—returns item at position i (as in array)  int size()—return number of items in vector  Using the template, write a short program which inserts 3 string items in a vector and prints out its contents.

Solution #include using namespace std; int main(){ vector v; v.push_back("Alice"); v.push_back("Brad"); v.push_back("Chad"); for (int i = 0; i < 3; i++){ cout << v[i]<< endl; } system("PAUSE"); return EXIT_SUCCESS; }

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