Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
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 and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
C++ for Engineers and Scientists Third Edition
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Main Index Contents 11 Main Index Contents Storage Containers -GeneralGeneral -Vectors (3 slides)Vectors -ListsLists -MapsMaps ADT’s ADT’s ADT’s (2 slides)Classes.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
Data Structures Using C++ 2E
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
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
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
C++ STL CSCI 3110.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CS342 Data Structures End-of-semester Review S2002.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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)
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
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.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
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.
Homework 4 questions???.
Standard Template Library (STL)
Chapter 4 Linked Lists.
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
7 Arrays.
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Presentation transcript:

Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers Adapter Classes Adapter Classes The List Container The List Container Stack Containers Stack Containers Queue Containers Queue Containers Priority Queue Containers Priority Queue Containers Set Containers Set Containers Map Containers Map Containers C++ Arrays C++ Arrays -Evaluating an Array as aEvaluating an Array as a Container The Generic Class The Generic Class Chapter 4 – The Vector Container Vectors -Vector Class Constructor –APIVector Class Constructor –API (2 slides) -Vector Class Operations –APIVector Class Operations –API (4 slides) -Output with VectorsOutput with Vectors -Declaring Vector ObjectsDeclaring Vector Objects -Adding and Removing VectorAdding and Removing Vector Elements -Resizing a VectorResizing a Vector The Insertion Sort The Insertion Sort -Insertion Sort AlgorithmInsertion Sort Algorithm (3 slides) Review - Template Class Review - Template ClassReview - Template Class Review - Template Class (2 slides) Store ClassStore Class Store Class (2 slides) Store Class Summary Slides Summary SlidesSummary Slides Summary Slides (6 slides)

Main Index Contents 22 Main Index Contents Container Types Sequence Containers Adapter Containers Associative Containers VectorStackSet, Multiset DequeQueue Map, Mutltimap ListPriority Queue

Main Index Contents 33 Main Index Contents Sequence Containers A sequence container stores data by position in linear order 1st element, 2nd element, and so forth.

Main Index Contents 44 Main Index Contents Associative Containers Associative containers store elements by key. – Ex: name, social security number, or part number. A program accesses an element in an associative container by its key, which may bear no relationship to the location of the element in the container.

Main Index Contents 5 Adapter Classes An adapter contains a sequence container as its underlying storage structure. The programmer interface for an adapter provides only a restricted set of operations from the underlying storage structure.

Main Index Contents 66 Main Index Contents The List Container Inserting into a List Container

Main Index Contents 77 Main Index Contents Stack Containers A stack allows access at only one end of the sequence, called the top.

Main Index Contents 88 Main Index Contents Queue Containers A queue is a container that allows access only at the front and rear of the sequence.

Main Index Contents 99 Main Index Contents Priority Queue Containers A priority queue is a storage structure that has restricted access operations similar to a stack or queue. Elements can enter the priority queue in any order. Once in the container, a delete operation removes the largest (or smallest) value.

Main Index Contents 10 Main Index Contents Set Containers A set is a collection of unique values, called keys or set members.

Main Index Contents 11 Main Index Contents Map Containers A map is a storage structure that implements a key- value relationship.

Main Index Contents 12 Main Index Contents C++ Arrays An array is a fixed-size collection of values of the same data type. An array is a container that stores the n (size) elements in a contiguous block of memory.

Main Index Contents 13 Main Index Contents Evaluating an Array as a Container The size of an array is fixed at the time of its declaration and cannot be changed during the runtime. – An array cannot report its size. A separate integer variable is required in order to keep track of its size. C++ arrays do not allow the assignment of one array to another. – The copying of an array requires the generation of a loop structure with the array size as an upper bound.

Main Index Contents 14 Main Index Contents Vectors

Main Index Contents 15 Generic Classes Structure template class templateClass { public: templateClass(const T& item); // constructor T f() const; // member function that return value of type T void g(const T& item); // member function that has an argument of type T... private: // data stored by the object T data // member function that return value of type T alue;... };

Main Index Contents 16 The store Template Class template class store { public: store(const T& item = T() ); // constructor // access and update functions T getValue() const; // return value void setValue( const T& item); // update value // overloaded operator << as a friend friend ostream& operator & obj) {//display output ostr << "Value = " << obj.value; return ostr; } private: T value; // data stored by the object };

Main Index Contents 17 Main Index Contents CLASS vector Constructors vector(); Create an empty vector. This is the default constructor. vector(int n, const T& value = T()); Create a vector with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T().

Main Index Contents 18 Main Index Contents CLASS vector Constructors vector(T *first, T *last); Initialize the vector using the address range [first, last). The notation *first and *last is an example of pointer notation that we cover in Chapter 5.

Main Index Contents 19 Main Index Contents CLASS vector Operations T& back(); Return the value of the item at the rear of the vector. Precondition:The vector must contain at least one element. const T& back() const; Constant version of back(). bool empty() const; Return true if the vector is empty and false otherwise.

Main Index Contents 20 Main Index Contents CLASS vector Operations T& operator[] (int i); Allow the vector element at index i to be retrieved or modified. Precondition:The index, i, must be in the range 0  i < n, where n is the number of elements in the vector. Postcondition:If the operator appears on the left side of an assignment statement, the expression on the right side modifies the element referenced by the index. const T& operator[] (int i) const; Constant version of the index operator.

Main Index Contents 21 Main Index Contents CLASS vector Operations void push_back(const T& value); Add a value at the rear of the vector. Postcondition:The vector has a new element at the rear and its size increases by 1. void pop_back(); Remove the item at the rear of the vector. Precondition:The vector is not empty. Postcondition:The vector has a new element at the rear or is empty.

Main Index Contents 22 Main Index Contents CLASS vector Operations void resize((int n, const T& fill = T()); Modify the size of the vector. If the size is increased, the value fill is added to the elements on the tail of the vector. If the size is decreased, the original values at the front are retained. Postcondition: The vector has size n. int size() const; Return the number of elements in the vector.

Main Index Contents 23 Output with Vectors // number of elements in list is v.size() template void writeVector(const vector & v) { // capture the size of the vector in n int i, n = v.size(); for(i = 0; i < n; i++) cout << v[i] << " "; cout << endl; }

Main Index Contents 24 Declaring Vector Objects // vector of size 5 containing the integer // value 0 vector intVector(5); // vector of size 10; each element // contains the empty string vector strVector(10);

Main Index Contents 25 Adding and Removing Vector Elements

Main Index Contents 26 Declaring a Vector int arr[5] = {7, 4, 9, 3, 1}; vector v(arr,arr+5); // v initially has 5 integers //Other option: good – when arr changes, vector declaration does not change int arr[ ] = {7, 4, 9, 3, 1, 3, 2}; int arrsize = sizeof(arr)/sizeof(int) /*sizeof returns the number of bytes of memory required for the type*/ vector v(arr,arr+5);

Main Index Contents 27 Resizing a Vector v.resize(10); // list size is doubled v.resize(4); // list is contracted. data // is lost

Main Index Contents 28 Main Index Contents

Main Index Contents 29 Main Index Contents The Insertion Sort

Main Index Contents 30 Main Index Contents Insertion Sort Algorithm insertionSort(): // sort a vector of type T using insertion // sort template void insertionSort(vector & v) { int i, j, n = v.size(); T temp; // place v[i] into the sublist v[0]... // v[i-1], 1 <= i < n, so it is in the // correct position

Main Index Contents 31 Main Index Contents Insertion Sort Algorithm for (i = 1; i < n; i++) { // index j scans down list from v[i] // looking for correct position to // locate target. assigns it to v[j] j = i; temp = v[i]; // locate insertion point by scanning // downward as long as temp < v[j-1] // and we have not encountered the // beginning of the list

Main Index Contents 32 Main Index Contents Insertion Sort Algorithm while (j > 0 && temp < v[j-1]) { // shift elements up list to make // room for insertion v[j] = v[j-1]; j--; } // the location is found; insert temp v[j] = temp; }

Main Index Contents 33 Main Index Contents Review - Template Class class templateClass { public: // constructor with argument of // constant reference type T templateClass (const T& item); // member function returns a value of // type T T f();

Main Index Contents 34 Main Index Contents Review - Template Class // member function has an argument of // type T void g(const T& item);... private: T dataValue;... };

Main Index Contents 35 Main Index Contents CLASS storeDeclaration“d_store.h” template class store { public: store(const T& item = T()); // initialize value with item or the default // object of type T getValue() const; // retrieve and return data member value

Main Index Contents 36 Main Index Contents CLASS storeDeclaration“d_store.h” void setValue(const T& item); // update the data member value to item friend ostream&operator & obj); // display output in the from "Value = " // value private: T value; // data stored by the object };

Main Index Contents 37 Main Index Contents Summary Slide 1 §- The Standard Template Library (STL) provides 10 container classes for solving a wide range of problems. §- Containers fall into one of three classifications: 1)sequence containers 2)adapters 3)associative containers.

Main Index Contents 38 Main Index Contents Summary Slide 2 §- The array data structure is a sequence container. -It defines a block of consecutive data values of the same type. -Arrays are direct access containers. §-An index may be used to select any item in the list without referencing any of the other items

Main Index Contents 39 Main Index Contents Summary Slide 3 §- The vector sequence container provides direct access through an index and grows dynamically at the rear as needed. -Insertion and deletion at the rear of the sequence is very efficient §-these operations inside a vector are not efficient.

Main Index Contents 40 Main Index Contents Summary Slide 4 §- The list sequence container stores elements by position. -List containers do not permit direct access §-must start at the first position (front) and move from element to element until you locate the data value. -The power of a list container is its ability to efficiently add and remove items at any position in the sequence.

Main Index Contents 41 Main Index Contents Summary Slide 5 adapter §- stacks and a queues are adapter containers that restrict how elements enter and leave a sequence. -A stack allows access at only one end of the sequence, called the top. -A queue is a container that allows access only at the front and rear of the sequence. §-Items enter at the rear and exit from the front. §- Similar to a stack or queue, the priority queue adapter container restricts access operations. §- Elements can enter the priority queue in any order. §- Once in the container, only the largest element may be accessed.

Main Index Contents 42 Main Index Contents Summary Slide 6 §- A set is a collection of unique values, called keys or set members. -The set container has a series of operations that allow a programmer to determine if an item is a member of the set and to very efficiently insert and delete items. §- A map is a storage structure that allows a programmer to use a key as an index to the data. -Maps do not store data by position and instead use key-access to data allowing a programmer to treat them as though they were a vector or array.