CSE 100: C++ TEMPLATES AND ITERATORS

Similar documents
CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

TEMPLATES AND ITERATORS

BINARY SEARCH TREES (CONTD) C++ TEMPLATES. Problem Solving with Computers-II

Dynamic Data Structures

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

Structuur van Computerprogramma s 2

Lesson 13 - Vectors Dynamic Data Storage

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Traversing Trees with Iterators

Traversing Trees with Iterators

List, Stack, and Queues

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

STL: C++ Standard Library

Tree Travsersals and BST Iterators

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

CSE 2123: Collections: Priority Queues. Jeremy Morris

CMSC 341 Lecture 10 Binary Search Trees

COEN244: Class & function templates

7 TEMPLATES AND STL. 7.1 Function Templates

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

LECTURE 11 TREE TRAVERSALS

Section 1: True / False (2 points each, 30 pts total)

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

STL Standard Template Library

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

CS11 Advanced C++ Fall Lecture 1

CS197c: Programming in C++

CSE 250: Data Structures Fall 2015 Sample Final Exam

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

Vectors. CIS 15 : Spring 2007

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Questions: ECE551 PRACTICE Final

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

Exceptions, Templates, and the STL

CSCI-1200 Data Structures Fall 2018 Lecture 23 Priority Queues II

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Trees, Part 1: Unbalanced Trees

Binary Trees and Binary Search Trees

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

Second Examination Solution

Largest Online Community of VU Students

Standard Template Library

Lecture-5. STL Containers & Iterators

Improving the Return Value of Erase-Like Algorithms

Computational Physics

1. The term STL stands for?

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CIS 190: C/C++ Programming. Lecture 12 Student Choice

Figure 1. A breadth-first traversal.

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

BINARY SEARCH TREES. Problem Solving with Computers-II

CS102 Binary Search Trees

TREES Lecture 12 CS2110 Fall 2016

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

- Alan Perlis. Topic 24 Heaps

CSE 12 Week Eight, Lecture One

CmpSci 187: Programming with Data Structures Spring 2015

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Object-Oriented Programming

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Short Notes of CS201

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

Tables, Priority Queues, Heaps

CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III

Solution to CSE 250 Final Exam

CSE100. Advanced Data Structures. Lecture 13. (Based on Paul Kube course materials)

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS201 - Introduction to Programming Glossary By

CSE 12 Basic Data Structures. Dr. Cynthia Bailey Lee

Lecture 15a Persistent Memory & Shared Pointers

Data Structures And Algorithms

Lecture 2. Binary Trees & Implementations. Yusuf Pisan

CSE 333 Midterm Exam 2/14/14

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CS 162, Lecture 25: Exam II Review. 30 May 2018

Software Development with C++ Templates

Data Structures - CSCI 102. CS102 Hash Tables. Prof. Tejada. Copyright Sheila Tejada

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Prefix Trees Tables in Practice

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

PIC 10A. Lecture 23: Intro to STL containers

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Cpt S 122 Data Structures. Templates

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

CSCI-1200 Data Structures Test 3 Practice Problem Solutions

CSCE 110 PROGRAMMING FUNDAMENTALS

Come and join us at WebLyceum

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

Transcription:

1 CSE 100: C++ TEMPLATES AND ITERATORS

2 Announcements Look out for the extra weekend section More on git and C++ (iterators) Live demo by your friendly tutors Not mandatory but it will be fun Bring your laptops to follow along

3 How is Assignment 1 going? A. I haven t looked at it. B. I ve read it, but I haven t done anything C. I ve gotten the code and possibly started looking at it/playing around with it. D. I ve implemented some of the required functions, but I m not done. E. I m done!

4 In Java: public class BSTNode { public BSTNode left; public BSTNode right; public BSTNode parent; public int data; public BSTNode( int d ) { data = d; C++, attempt 5: class BSTNode { BSTNode* left; BSTNode* right; BSTNode* parent; int const data; BSTNode( const int & d ) : data(d) { left = right = parent = 0; ; ALWAYS initialize in C++. C++ won t do it for you. Why not? What if we don t want to be stuck with ints?

5 BST, with templates: template<typename Data> class BSTNode { BSTNode<Data>* left; BSTNode<Data>* right; BSTNode<Data>* parent; Data const data; ; BSTNode( const Data & d ) : data(d) { left = right = parent = 0;

6 BST, with templates: template<typename Data> class BSTNode { BSTNode<Data>* left; BSTNode<Data>* right; BSTNode<Data>* parent; Data const data; A. How would you create a BSTNode object on the runtime stack? ; BSTNode( const Data & d ) : data(d) { left = right = parent = 0;

7 BST, with templates: template<typename Data> class BSTNode { BSTNode<Data>* left; BSTNode<Data>* right; BSTNode<Data>* parent; Data const data; B. How would you create a pointer to BSTNode with integer data? ; BSTNode( const Data & d ) : data(d) { left = right = parent = 0;

8 BST, with templates: template<typename Data> class BSTNode { BSTNode<Data>* left; BSTNode<Data>* right; BSTNode<Data>* parent; Data const data; C. How would you create an BSTNode object on the heap? ; BSTNode( const Data & d ) : data(d) { left = right = parent = 0;

9 BST, with templates: template<typename Data> class BSTNode { BSTNode<Data>* left; BSTNode<Data>* right; BSTNode<Data>* parent; Data const data; BSTNode( const Data & d ) : data(d) { left = right = parent = 0; ; BSTNodes will be used in a BST, and with a BSTIterator

10 Working with a BST template<typename Data> class BST { private: /** Pointer to the root of this BST, or 0 if the BST is empty */ BSTNode<Data>* root; /** Default constructor. Initialize an empty BST. */ BST() : root(nullptr){ void insertasleftchild(bstnode<data>* parent, const Data & item) { // Your code here

11 Working with a BST: Insert void insertasleftchild(bstnode<data>* parent, const Data & item) { // Your code here Which line of code correctly inserts the data item into the BST as the left child of the parent parameter. A. parent.left = item; B. parent->left = item; C. parent->left = BSTNode(item); D. parent->left = new BSTNode<Data>(item); E. parent->left = new Data(item);

12 CHANGING GEARS: C++STL and BSTs The C++ Standard Template Library is a very handy set of built-in data structures (containers), including: array vector deque forward_list list stack queue priority_queue set multiset (non unique keys) unordered_set map unordered_map multimap bitset Of these, set is one that is implemented using a balanced binary search tree (typically a red-black tree)

Imagining ourselves as C++ STL class designers set s find function has this prototype: 13 template <typename T> class set { iterator find ( T const & x ) const; What does the final const in the function header above mean? A. find cannot change its input argument B. find cannot change the value of its input argument (x) C. find cannot change the underlying set

Imagining ourselves as C++ STL class designers set s find function has this prototype: 14 template <typename T> class set { iterator find ( T const & x ) const; The documentation for set s find function says: Searches the container for an element with a value of x and returns an iterator to it if found, otherwise it returns an iterator to the element past the end of the container.

15 C++ STL Iterators What is an iterator? In the iterator pattern of OO design, a container has a way to supply to a client an iterator object which is to be used by the client to access the data in the container sequentially, without exposing the container s underlying representation.

16 Iterator class template for BST template <typename T> class BSTIterator { private: Node<T>* curr; /** Constructor */ BSTIterator(Node<T>* n) : curr(n) {

17 C++ STL Iterators set<int> c;... // get an iterator pointing to container s first element set<int>::iterator itr = c.begin(); What do you think begin() returns? A. The address of the root in the set container class B. The address of the node with the smallest data key C. The address of the smallest data key D. None of the above

18 C++ STL Iterators set<int> c;... // get an iterator pointing to container s first element set<int>::iterator itr = c.begin(); // get an iterator pointing past container s last element set<int>::iterator end = c.end(); // loop while itr is not past the last element while(itr!= end) { cout << *itr << endl; // dereference the itr to get data ++itr; // increment itr to point to next element Circle/list the functions that the iterator has to implement.

19 template<typename Data> class BSTIterator : public std::iterator<std::input_iterator_tag,data> { private: BSTNode<Data>* curr; /** Constructor. Use the argument to initialize the current BSTNode * in this BSTIterator. */ // TODO BSTIterator(BSTNode<Data>* curr) { // TODO /** Dereference operator. */ Data operator*() const { return curr->data; /** Pre-increment operator. */ BSTIterator<Data>& operator++() { curr = curr->successor(); return *this; In which class is successor() defined? A. BST B. BSTNode C. BSTIterator D. Data

20 C++ STL Iterators set<int> c;... // get an iterator pointing to container s first element set<int>::iterator itr = c.begin(); // get an iterator pointing past container s last element set<int>::iterator end = c.end(); // loop while itr is not past the last element while(itr!= end) { cout << *itr << endl; // dereference the itr to get data ++itr; // increment itr to point to next element What kind of traversal is the above code doing? A. In order B. Pre order C. Post order D. None of the above