Chapter 11 Generic Collections

Slides:



Advertisements
Similar presentations
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.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter6 LISTS AND STRINGS. Outline 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked.
Linked Lists CENG 213 Data Structures.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
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.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 Data Structures and Algorithms Stacks and Queues.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Data Abstraction and Object-Oriented Design Problem Solving,
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
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.
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.
16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CPSC 252 Templatization Page 1 Templatization In CPSC152, we saw a class vector in which we could specify the type of values that are stored: vector intData(
Data Abstraction: The Walls
Programming with ANSI C ++
EECE 310: Software Engineering
Chapter 18: Stacks and Queues.
Computing Fundamentals with C++
Chapter 14 Templates C++ How to Program, 8/e
C++ Standard Library.
The Bag and Sequence Classes with Linked Lists
Programming with Recursion
Chapter 10: An Array Instance Variable
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Object Oriented Programming COP3330 / CGS5409
Associative Structures
What’s Left in the Course
Chapter 15 Pointers, Dynamic Data, and Reference Types
Building Java Programs
Building Java Programs
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 13 Vector of Vectors (2D Arrays)
14 – Sequential Containers
Chapter 6 Class Definitions and Member Functions
CMSC 202 Lesson 22 Templates I.
Chapter 12 Pointers and Memory Management
Chapter 4 Implementing Free Functions
Lecture 2: Implementing ArrayIntList reading:
Chapter 7 The Java Array Object © Rick Mercer.
Computing Fundamentals with C++
Templates I CMSC 202.
CS150 Introduction to Computer Science 1
the Standard Template Library
slides created by Ethan Apter and Marty Stepp
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
SPL – PS1 Introduction to C++.
Presentation transcript:

Chapter 11 Generic Collections 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates

Chapter 11 A Container with Iterators Build your own collection class to store any type of element Better understand classes with data members, constructors, and member functions Better understand how to develop functions that involve vector processing

Collection classes Programmers often use many collections Collection classes have the main purpose of storing a collection of elements Standard collection classes include vector<type>, stack<type>, queue<type>, list<type> All of these take a type argument, which is the type of elements that the collection stores

Passing Types as Arguments C++ has a template mechanism that allow the type of element to be set when compiled It is a type enclosed two special symbols <type> This allows us to have a vector of int, double, string, BankAccount, . . . The collection can only store that type of element With a type argument, we only need one collection class In this presentation, a Bag type is implemented with templates to allow for Bag<int> aBag;

class Bag<Type> The Bag class developed here will review class definitions, vector processing, and show it is possible to pass a type like int or double as an argument A bag (aka multi-set) is the most general collection Bags store a collection of elements not in any particular order and are not necessarily unique operations include bag::add bag::remove bag::occurrencesOf bag::size

Using a Bag object This code should compile, all assertions should pass Bag<int> aBag; aBag.add(5); aBag.add(4); assert(aBag.occurrencesOf(5) == 1); assert(aBag.occurrencesOf(4) == 2); assert(aBag.occurrencesOf(99) == 0); assert(!aBag.remove(99)); assert(aBag.remove(5)); assert(aBag.occurrencesOf(5) == 0); assert(aBag.remove(4)); assert(aBag.occurrencesOf(4) == 1);

The Data Members and Constructor // File name: Bag.h #include <vector> template<class Type> // Allow type arguments class Bag { private: std::vector<Type> elements; // Can be any type int n; public: //--constructor Bag() { elements.resize(20); // size 20 is arbitrary n = 0; }

Bag::add The Bag::add operation adds all new elements to the "end" of the vector. The vector may be resized // Add element and increase the size (n) by 1 void add(Type const& element) { // First make sure there is enough capacity if (n == elements.size()) { // Grow the vector's capacity by 10 elements.resize(n + 10); } // Then add element at the end of the vector elements[n] = element; // Increase the number of elements n++;

Bag::size The Bag::size operation simply returns n, that increases by 1 in add and will decrease by 1 in remove // Return the number of elements // that are currently in this Bag int size() const { return n; }

Bag::remove The Bag::remove operation begins by finding the index of the value to be removed // pre: removalCandidate must define == // post: If found, value is removed from this Bag. // If object is not in this Bag, return false. bool remove(Type const& value) { // Find the index of the element to remove // or let index be out of range when not found int index = 0; while (index < n && value != elements[index]) { index++; } // . . .

Bag::remove If not found, return false. If found, overwrite it with the most recently added element // element[subscript] == value if found, // otherwise subscript == size (not found). if (index == n) { return false; } else { // Overwrite value with the last element elements[index] = elements[n - 1]; // and decrease size by 1 n--; // Report success to the client return true; } // End remove member function

Trace Bag::remove Assume this state of aBag<int> where n==4 After aBag.remove(4) when n-- makes n==3 vector location value element[0] 5 element[1] 4 element[2] element[3] 9 vector location value element[0] 5 element[1] 4 9 element[2] 4 element[3] 9 This 9 is no longer in the Bag since n == 3

Bag::occurrencesOf Bag::occurrencesOf iterates over the vector to count how often value exists in this Bag // Return how often value exists in this Bag int occurrencesOf(Type const& value) const { int result = 0; for (int i = 0; i < n; i++) { if (value == elements[i]) result++; } return result;