Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
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ă.
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.
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.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
Templates and the STL.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
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.
Generic Programming Using the C++ Standard Template Library.
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.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
The List ADT Reading: Sections 3.2, 3.3, 3.5.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
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.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Standard Template Library a collection of useful tools.
Data Structure and Algorithm Analysis 03: Lists, Stacks and Queues Hongfei Yan School of EECS, Peking University.
Object-Oriented Programming (OOP) Lecture No. 41
CS212: Object Oriented Analysis and Design
Cpt S 122 – Data Structures Abstract Data Types
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Standard Template Library (STL)
STL Common tools for C++.
Starting Out with C++ Early Objects Eighth Edition
Trees 4 The B-Tree Section 4.7
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Collections Intro What is the STL? Templates, collections, & iterators
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CMSC 341 Lecture 5 Stacks, Queues
ADT Implementations: Templates and Standard Containers
Object Oriented Programming COP3330 / CGS5409
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
priority_queue<T>
Doubly Linked List Implementation
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Containers: Queue and List
Standard Template Library
STL List.
Collections Intro What is the STL? Templates, collections, & iterators
Doubly Linked List Implementation
Standard Template Library
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
The List Container and Iterators
STL List.
Data Structures & Programming
Presentation transcript:

Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4 Introduction Welcome to COP 4530, Data Structures, Algorithms, and Generic Programming. I hope very much, and sincerely believe, that this course will change you in ways that are deeply satisfying. So far, you have acquired proficiency in programming. This course will start the process of your transformation from a programmer to a computer scientist. One of the important tasks of a computer scientist is to make efficient use of computational resources. This course will teach you about different ways of organizing the data to facilitate such efficient use, and will also discuss efficient techniques to perform some fundamental operations in computer science. A subsequent course on Algorithms, COP 4531, will teach you to use the techniques discussed in this course to solve commonly encountered computer science problems efficiently. Both these courses will also teach you to analyze the efficiency, and prove the correctness, of your program in a mathematically rigorous manner. Material you learn in these two courses is critical to your becoming a good software developer later.

Abstract Data Type (ADT) High-level definition of data types An ADT specifies A collection of data A set of operations on the data or subsets of the data ADT does not specify how the operations should be implemented Examples vector, list, stack, queue, deque, priority queue, table (map), associative array, set, graph, digraph

Iterators Helps navigate through the items in a list. An example: iterator over vector v. for (int i = 0; i != v.size(); i++ ) cout << v[i] << endl;

Iterators (contd.) A generalized type that help in navigating any container A way to initialize at the front and back of a list A way to move to the next or previous position A way to detect the end of an iteration A way to retrieve the current value Examples: Iterator type for vector<int> defined as vector<int>::iterator itr; Iterator type for list<string> defined as list<string>::iterator itr;

Getting an Iterator Two methods in all STL containers Example: begin() Returns an iterator to the first item in the container end() Returns an iterator representing the container (i.e. the position after the last item) Example: for (int i = 0; i != v.size(); i++ ) cout << v[i] << endl; can be written using iterators as for(vector<int>::iterator itr=v.begin(); itr!=v.end(); itr.???) cout << itr.??? << endl; What about ???

Iterator Methods Iterators have methods Many methods use operator overloading itr++ and ++itr advance the iterator to next location *itr  return reference to object stored at iterator itr’s location itr1 == itr2 true if itr1 and itr2 refer to the same location, else false itr1 != itr2 true if itr1 and itr2 refer to different locations, else false Previous example becomes for(vector<int>::iterator itr= v.begin(); itr!= v.end(); itr++) cout << *itr << endl; Alternatively vector<int>::iterator itr = v.begin( ); while( itr != v.end( ) ) cout << *itr++ << endl;

Iterator example

const_iterator Returns a constant reference for operator* So that a function does not try to modify the elements of a constant container object. Note that c.begin() and c.end() functions in the example return const_iterator type.

The Vector ADT Generic arrays

The Vector ADT Extends the notion of array by storing a sequence of arbitrary objects Elements of vector ADT can be accessed by specifying their index.

Vectors in STL Collection  Elements of some proper type T Operations int size()  returns the number of elements in the vector void clear()  removes all elementes from the vector bool empty()  returns true of the vector has no elements void push_back ( const Object &x ) adds x to the end of the vector void pop_back ( ) Removes the object at the end of the vector Object & back ( ) Returns the object at the end of the vector Object & front ( ) Returns the object at the front of the vector

Vectors in STL (contd.) More Operations Object & operator[] ( int index ) Returns the object at location index (without bounds checking) Both accessor and mutator versions Object & at( int index ) Returns the object at location index (with bounds checking) int capacity() Returns the internal capacity of the vector void reserve(int newCapacity) Sets the new capacity of the vector void resize(int newSize ) Change the size of the vector

Implementing Vector Class Template Vector maintains A primitive C++ array The array capacity The current number of items stored in the Vector Operations: Copy constructor operator= Destructor to reclaim primitive array. All the other operators we saw earlier.

Vector Implementation (Part 1) Constructor Copy Constructor deep copy assignment

Vector Implementation (Part 2) Expand to twice as large because memory allocation is an expensive operation

Vector Implementation (Part 3) No error checking

Vector Implementation (Part 4) Same as pointers to Object