11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

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.
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Lists: An internal look
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.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Beginning C++ Through Game Programming, Second Edition
9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Templates and the STL.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 CSC 222: Computer Programming II Spring 2004 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search Class design  templated.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
Containers Overview and Class Vector
13-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
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.
18-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
 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.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
2-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
C++ Classes and Data Structures Jeffrey S. Childs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
10-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Pointers and Dynamic Arrays
Pointers and Linked Lists
Computing Fundamentals with C++
Pointers and Linked Lists
Chapter 10: An Array Instance Variable
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
CSC 143 Queues [Chapter 7].
Chapter 6 Class Definitions and Member Functions
Chapter 11 Generic Collections
Computing Fundamentals with C++
Getting queues right … finally (?)
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Presentation transcript:

11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Computing Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer are welcome to use this presentation as long as this copyright notice remains intact.

11-2 Chapter 11 A Container with Iterators  Chapter Objectives  declare and use bag objects that store collections of objects  define classes with vector data members  implement member functions that manipulate vectors  implement and use iterator functions for traversing over all the items in a collection

The bag class  Programmers use many container classes  A class with the main purpose of storing a collection of elements that can be accessed  different container classes allow different access –for example a vector allows direct access to any element  Other standard containers: stack, queue, list, and map  The bag class is presented here as review of  class definitions  vectors and vector processing  It's a container class capable of storing many items

11-4 A bag class continued  A bag object as discussed in Chapter 11--there are others  stores a collection of elements that  are not in any particular order  are not necessarily unique  has operations that include bag::add bag::add bag::remove bag::remove bag::capacity bag::capacity bag::size bag::size  class definition for this bag class follows

11-5 class bag { public://--constructors bag(); bag(); bag(int capacity); bag(int capacity);//--modifiers void add(BAG_ELEMENT_TYPE newElement); void add(BAG_ELEMENT_TYPE newElement); bool remove(BAG_ELEMENT_TYPE removalCandidate); bool remove(BAG_ELEMENT_TYPE removalCandidate);//--accessors int capacity(); int capacity(); int size(); int size(); bool isEmpty(); bool isEmpty(); // -- iterator functions on next slide private: vector my_data; vector my_data; int my_size; int my_size;}; The bag class definition

11-6 The public iterator functions void first(); // post: my_index points to the first item, or if the // bag is empty, isDone would now return true void next(); // post: point to the next item, or isDone returns true bool isDone() const; // post: Returns true if collection has been traversed BAG_ELEMENT_TYPE currentItem() const; // post: Returns the item pointed to by the my_index

11-7 Client code that uses bag #include #include using namespace std; // This must precede #include "bag" (better way shown later) typedef string BAG_ELEMENT_TYPE; // <- for a bag of strings #include "bag" int main() { // This program sends all possible messages to a bag object bag bagOfStrings; bag bagOfStrings; bagOfStrings.add( "A string" ); bagOfStrings.add( "A string" ); bagOfStrings.add( "Another string" ); bagOfStrings.add( "Another string" ); bagOfStrings.add( "and still another" ); bagOfStrings.add( "and still another" ); bagOfStrings.add( "and a fourth" ); bagOfStrings.add( "and a fourth" ); bagOfStrings.remove( "and still another" ); // delete one bagOfStrings.remove( "and still another" ); // delete one bagOfStrings.remove( "I'm not in the bag!" ); // can't bagOfStrings.remove( "I'm not in the bag!" ); // can't return 0; return 0;}

11-8 A bag of ints  A bag object stores a collection objects  To store int objects for example, do this: typedef int BAG_ELEMENT_TYPE; typedef int BAG_ELEMENT_TYPE; #include "bag" #include "bag" int main() int main() { bag bagOfInts( 100 ); bag bagOfInts( 100 ); bagOfInts.add( 72 ); bagOfInts.add( 72 ); bagOfInts.add( 97 ); bagOfInts.add( 97 ); //... //...

The bag Constructors  The constructors create an empty bag bag::bag() { // Default is an empty bag with capacity 16 my_size = 0; my_size = 0; my_data.resize(16); my_data.resize(16);} bag::bag(int initialCapacity) { // Create an empty bag of any capacity desired my_size = 0; my_size = 0; my_data.resize(initialCapacity); my_data.resize(initialCapacity);} bag default; // call default constructor bag default; // call default constructor bag hold500(500); // construct with one argument bag hold500(500); // construct with one argument

The bag Modifying Member Functions  The bag::add operation adds all new elements to the "end" of the vector. If there is no room, the vector is resized void bag::add(BAG_ELEMENT_TYPE newElement) { // First, increase the bag capacity if necessary if(my_size >= my_data.capacity()) if(my_size >= my_data.capacity()) { my_capacity = 2 * my_data.capacity(); my_capacity = 2 * my_data.capacity(); my_data.resize( my_capacity ); my_data.resize( my_capacity ); } // Store argument into vector of BAG_ELEMENT_TYPEs... // Store argument into vector of BAG_ELEMENT_TYPEs... my_data[my_size] = newElement; my_data[my_size] = newElement; //...and make sure my_size is always increased by +1: //...and make sure my_size is always increased by +1: my_size++; my_size++;}

11-11 bag::remove  bag::remove uses sequential search to find the element to be removed  If the element is found  move the last element ( my_data[my_size-1] ) into the location of the removal element  decrease my_size by 1 does the following code look familiar?

11-12 bag::remove bool bag::remove(BAG_ELEMENT_TYPE removalCandidate) { int subscript = 0; int subscript = 0; // Sequentially search for removalCandidate // Sequentially search for removalCandidate while( (subscript < my_size) while( (subscript < my_size) && (my_data[subscript] != removalCandidate)) { && (my_data[subscript] != removalCandidate)) { subscript++; subscript++; } if(subscript == my_size) // removalCandidate not found if(subscript == my_size) // removalCandidate not found return false; return false; else { // move last element to removalCandidate's spot else { // move last element to removalCandidate's spot my_data[subscript] = my_data[my_size-1]; my_data[subscript] = my_data[my_size-1]; // and then decrease size by 1 // and then decrease size by 1 my_size--; my_size--; // report success to the message sender // report success to the message sender return true; return true; }}

11-13 The state of bagOfStrings before removing "Another string" Data Members State my_data[0] "A string" my_data[1] "Another string" my_data[2] "and still another" my_data[3] "and a fourth" … my_size 4 local objects in bag::remove removalCandidate "Another string" subscript 1

11-14 "Another string" The state after removing "another string" Wipe out "Another string" No longer meaningful Decrease size 1. Find removalCandidate in my_data[1] 2. Overwrite it with the "last" bag element and decrease size my_data[0] "A string" my_data[1] "And a fourth" my_data[2] "and still another" my_data[3] "And a fourth"... my_size 3

The bag Accessor functions int bag::capacity() const { // Return how many elements could be stored return my_data.capacity(); return my_data.capacity();} int bag::size() const { // Return how many elements are in the bag return my_size; return my_size;} bool bag::isEmpty() const { // Return true if there are 0 elements in the bag return my_size == 0; return my_size == 0;}

The Iterator Pattern  vectors have subscripts for iterating over all the meaningful elements  standard containers, including vectors use a separate iterator class more later  bags use member functions firstisDonecurrentItemnext

11-17 Iterating over items in a bag bag aBag; int sum = 0; int sum = 0; int n = 9; int n = 9; for(int j = 1; j <= n; j=j+2) for(int j = 1; j <= n; j=j+2) { aBag.add(j); aBag.add(j); } cout << "Iterate over the entire collection" << endl; cout << "Iterate over the entire collection" << endl; for( aBag.first(); ! aBag.isDone(); aBag.next() ) for( aBag.first(); ! aBag.isDone(); aBag.next() ) { cout.width(4); cout.width(4); // reference "current" bag element // reference "current" bag element cout << aBag.currentItem(); cout << aBag.currentItem(); // reference it again // reference it again sum = sum + aBag.currentItem(); sum = sum + aBag.currentItem(); } cout << "\nSum: " << sum << endl; cout << "\nSum: " << sum << endl; Output ? Demonstrate testbag.cpp

11-18 The iterator implementations void bag::first() { // Set internal index to reference the 1st element my_index = 0; my_index = 0;} bool bag::isDone() const { // Return true if previous next was the last element return my_index >= my_size; return my_index >= my_size;} void bag::next() { // Set internal index to reference the next element // or to allow isDone to return true // or to allow isDone to return true my_index++; my_index++;} BAG_ELEMENT_TYPE bag::currentItem() const { // Set internal index to reference the 1st element return my_data[my_index]; return my_data[my_index];}

11-19 The Iterator pattern will be repeated as are all patterns  The iterator pattern will be used again  In Chapter 13: Object-Oriented Software Development  to display all CDs in a CD collection  to display all track in a CD  In Section 14.2  iterator objects are used to traverse the elements in the standard C++ containers such as vector and list –the iterator pattern is implemented quite differently