ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.

Slides:



Advertisements
Similar presentations
Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup
Advertisements

SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Practice with C++: Writing a concrete class ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Pointers1 Pointers & Dynamic Arrays Allocating memory at run-time.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Templates and the STL.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
Data Structures Using C++ 2E
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
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 6 – September 6 th, 2001.
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.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
Generic Programming Using the C++ Standard Template Library.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
C++ STL CSCI 3110.
Pointers OVERVIEW.
Chapter 19 Vectors, templates, and exceptions John Keyser’s Modifications of Slides By Bjarne Stroustrup
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 9 – September 18, 2001.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 19 Vectors, templates, and exceptions Hartmut Kaiser
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
CS212: Object Oriented Analysis and Design Lecture 24: Introduction to STL.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 10 – September 20, 2001.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Templates&STL. Computer Programming II 2 Introduction They perform appropriate operations depending on the data type of the parameters passed to them.
Lecture 7 : Intro. to STL (Standard Template Library)
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
A gentle introduction to the standard template library (STL) Some references:
1 CSC 222: Computer Programming II Spring 2004 Pointers and dynamic memory  pointer type  dereference operator (*), address-of operator (&)  sorting.
The Standard Template Library Container Classes Version 1.0.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
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.
Vectors CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
CSCE Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 18: Vectors, Templates and Exceptions 1.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Object-Oriented Programming (OOP) Lecture No. 41
Vectors Holds a set of elements, like an array
Collections Intro What is the STL? Templates, collections, & iterators
What remains Topics Assignments Final exam
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
Jordi Cortadella and Jordi Petit Department of Computer Science
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001

ICOM 4035Dr. Manuel Rodriguez Martinez2 Readings Read Handout about Container Classes –Available from Engineering Reproduction Center as ICOM-4035-Manual # 3

ICOM 4035Dr. Manuel Rodriguez Martinez3 Container Classes A container class is a class used to create objects that store collections of other objects. –Examples: List of names in a video store. Set of integers that appear in a login name. A container class is a data structure. There are many types of containers, depending on the way that the objects are organized within the container. –Vector – elements are not sorted –Set – elements are unique (no duplicates) –Sequence – elements are unique and are sorted

ICOM 4035Dr. Manuel Rodriguez Martinez4 Container Classes (cont.) C++ provides the Standard Template Library which includes several container classes. –Vector –Deque –List –Set –Multi-Set (Bag) –Map (Hash Table) –Multi-Map Good containers should be re-usable –By using typedef –Templates – preferred way.

ICOM 4035Dr. Manuel Rodriguez Martinez5 Vector Container Enhancement over the built-in array –C++ books recommend vector over built-in array The vector has the following features 1.It knows its size – keeps track of number of elements 2.Can grow or shrink on demand – the number of elements can be reallocated to make it larger or smaller dynamically 3.Can perform index bounds checking – can catch error when you try to access illegal elements (e.g. v[-1]) –Not all implementations do this, for example STL performs no bounds checking because of the overhead incurred.

ICOM 4035Dr. Manuel Rodriguez Martinez6 Vector Organization … vect_size vect_capacity vect_elems vect_size – number of element (probably) in use vect_capacity – number of elements allocated (max size) vect_elems – pointer to the array of elements Vector class

ICOM 4035Dr. Manuel Rodriguez Martinez7 Vector Methods Constructors –Based on size – vector(int init_size) –Copy constructor – vector(const vector& vect) Destructor –Need to free pointer to elements Accessors –Get the current size –Get the current capacity –Access element i in the vector Mutators –Copy assignment –Resize the vector –Reallocate the vector –Add a new element at the end and make vector grow if need be –Assign element i in the vector

ICOM 4035Dr. Manuel Rodriguez Martinez8 Vector class interface (vector.h) #ifndef _VECTOR_H #define _VECTOR_H #include “Object.h” class vector{ public: explicit vector(int init_size = 0); vector(const vector& vect); ~vector(); const vector& operator=(const vector& vect); Object& operator[](int index); const Object& operator[](int index) const; int size() const; int capacity() const; void resize(int new_size); void reserve (int new_capacity); private: int vect_size; Int vect_capacity; Object *vect_elems; }; #endif vector.h #ifndef _OBJECT_H #define _OBJECT_H typedef int Object #endif Object.h

ICOM 4035Dr. Manuel Rodriguez Martinez9 Vector implementation (vector.cc) #include “vector.h” #include // constructor based on size vector::vector(int init_size) : vect_size(init_size), vect_capacity(init_size){ vect_elemns = new Objects[vect_capacity]; } // copy constructor vector::vector(const vector& vect) :vect_size(vect.size()), vect_capacity(vect.capacity()), vect_elems (NULL) { int i=0; vect_elems = new Objects[vect_capacity]; for (i=0; i < vect_size; ++i){ vect_elems[i] = vect[i]; } // destructor vector::~vector() { delete [] vect_elems; vect_size = vect_capacity = 0; }

ICOM 4035Dr. Manuel Rodriguez Martinez10 Vector Implementation II (vector.cc) const vector& vector::operator=(const vector& vect){ int i=0; if (this != &vect){ vect_size = vect.size(); vect_capacity = vect.capacity(); // recycle old elements delete [] vect_elems; // reallocate new elements vect_elems = new Objects[vect_capacity]; for (i=0; i < vect_size; ++i){ vect_elems[i] = vect[i]; } return *this; } int vector::size() const{ return vect_size; } int vector::capacity() const{ return vect_capacity; }

ICOM 4035Dr. Manuel Rodriguez Martinez11 Vector Implementation III (vector.cc) void vector::reserve(int new_capacity){ Object *old = vect_elems; int num_copy = 0; assert(new_capacity > 0); num_copy = (new_capacity < vect_size ? new_capacity : vect_size); vect_capacity = new_capacity; vect_size = num_copy; vect_elems = new Objects[vect_capacity]; for(i=0; i < num_copy; ++i){ vect_elems[i] = old[i]; } delete [] old; } void vector::resize(int new_size){ assert(new_size > 0); if (new_size > vect_capacity){ reserve(new_size * 2); } vect_size = new_size; }

ICOM 4035Dr. Manuel Rodriguez Martinez12 Vector Implementation IV (vector.cc) void vector::push_back(const Object& new_obj){ if (vect_size == vect_capacity){ resize(vect_capacity * 2 + 1); } vect_elems[vect_size++] = new_obj; } Object& vector::operator[](int index){ assert(((index >=0) && (index < vect_capacity)); return vect_elems[index]; } const Object& operator[](int index) const { assert(((index >=0) && (index < vect_capacity)); return vect_elems[index]; }

ICOM 4035Dr. Manuel Rodriguez Martinez13 Using the vector class #include #include “vector.h” int main(){ vector v; int n=0, num=0, sum=0; cout << "This program adds numbers" << endl; cout << "How many number to add: " << endl; cin >> num; v = vector(num); cout << "Start typing the numbers" << endl; for (int i=0; i < num; ++i){ cin >> n; vect.push_back(n); // can also use: v[i] = n } // add them; for (i=0; i < vect.size(); ++i){ sum += v[i]; } cout << "Total sum is : " << sum << endl; return 0; }

ICOM 4035Dr. Manuel Rodriguez Martinez14 Bag Class Container Bag is data structure used to store elements with the following semantics: –Copies of the same element can stored in the bag B. –A find operation is supported to determine if an element x is present in the bag B. –An erase operation is supported to erase an instance of an element x in the bag B. –An erase all operation is supported to erase all instances of an element x in the bag B –A union operation is supported to concatenate the contents of two bags.

ICOM 4035Dr. Manuel Rodriguez Martinez15 Bag Class Conceptual Example Bag of integers Unsed part Operation: B.count(8) = 1 B.count(5) = 2 B.count(7) = 0

ICOM 4035Dr. Manuel Rodriguez Martinez16 Bag Class Design … bag_size bag_capacity bag_elemsBag class Wait a minute!!! We can use vector for this! add more semantics to implement: count() insert() erase()

ICOM 4035Dr. Manuel Rodriguez Martinez17 Bag Class Methods Constructor –Make an empty bag –Make a bag from another bag (copy constructor) Accessor –Get current size –Get current capacity –Get the count of an element Mutators –Insert a new element –Erase one instance of element x –Erase all instances of element x Non-member –Addend all elements from two bags to create a new one.