TEMPLATES AND ITERATORS

Similar documents
CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS

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

The Standard Template Library. An introduction

Templates & the STL. CS 2308 :: Fall 2015 Molly O'Neil

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

Standard Template Library

CSCI-1200 Data Structures Spring 2016 Lecture 22 Priority Queues, part II (& Functors)

STL Quick Reference for CS 241

Improve your C++! John Richardson. Lab Study Meeting 2013

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

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

Initializer Lists for Standard Containers

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

Lecture 4a Using The Standard Template Library. Jack Applin, Guest Lecturer

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

CS11 Advanced C++ Fall Lecture 1

Computational Physics

LECTURE 11 TREE TRAVERSALS

Improving the Return Value of Erase-Like Algorithms

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Module 9. Templates & STL

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

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

G52CPP C++ Programming Lecture 18

Lesson 13 - Vectors Dynamic Data Storage

CS24 Week 4 Lecture 2

Dynamic Data Structures

Object-Oriented Programming for Scientific Computing

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

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

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

COEN244: Class & function templates

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

Spare Matrix Formats, and The Standard Template Library

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Introduction to C++ Introduction to C++ 1

STL Standard Template Library

COMP6771 Advanced C++ Programming

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Vectors. CIS 15 : Spring 2007

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

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

7 TEMPLATES AND STL. 7.1 Function Templates

Structuur van Computerprogramma s 2

C++ Standard Template Library

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

Chapter 5. The Standard Template Library.

More on Templates. Shahram Rahatlou. Corso di Programmazione++

CS197c: Programming in C++

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

Lecture-5. STL Containers & Iterators

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

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

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

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

1 Short Answer (7 Points Each)

Containers and the Standard Template Library (STL)

CS 520/620 Advanced Software Engineering Spring February 02, 2016

Lecture 15a Persistent Memory & Shared Pointers

1. The term STL stands for?

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

C++ Templates. David Camp

GridKa School 2013: Effective Analysis C++ Standard Template Library

Figure 1. A breadth-first traversal.

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

CSCI-1200 Data Structures Fall 2015 Lecture 24 Hash Tables

Major Language Changes, pt. 1

Sequential Containers. Ali Malik

Programming with Haiku

EL2310 Scientific Programming

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Announcements/Follow-ups

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

Container Class and Integrators, Proxy Class EC6301-OOPS AND DATA STRUCTURES

List Iterator Implementation

CSCI-1200 Data Structures Fall 2017 Lecture 23 Functors & Hash Tables, part II

Standard Library Reference

Chapter 21a Other Library Issues

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

STL. Zoltán Porkoláb: C++11/14 1

Exercise 6.2 A generic container class

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

CSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

EL2310 Scientific Programming

Splicing Maps and Sets (Revision 1)

Exceptions, Templates, and the STL

Document Number: P0429R4 Date: Reply to: 0.1 Revisions... 1

EL2310 Scientific Programming

The Standard Template Library Classes

Linked lists Tutorial 5b

Transcription:

TEMPLATES AND ITERATORS Problem Solving with Computers-I https://ucsb-cs24-sp17.github.io/

2 Announcements Checkpoint deadline for pa04 (aka lab05) is due today at 11:59pm Be sure to push your code to github AND Submit on submit.cs

3 How is pa04 going? A. Going well B. I am working on passing test1() C. I am stuck and don t know how to proceed D. I haven t started

4 Demo Converting the intlist class (linked list) to a class that uses templates

5 Linked list, with templates: template<class Item> class Node { public: Node<Item>* next; Item const data; Node( const Item & d ) : data(d) { next = 0; How would you create a Node object on the runtime stack? int myint =10; A. Node n(myint); B. Node<int> n; C. Node<int> n(myint); D. Node<int> n = new Node<int>(myInt); ;

6 Linked list, with templates: template<class Item> class Node { public: Node<Item>* next; Item const data; Write a line of code to create a new Node object with int data on the heap and make nodeptr to point to it. int myint=10; ; Node( const Item & d ) : data(d) { next = 0;

7 Automatic type deduction with auto Linked list, with templates: auto p = new Node<int>(myInt); template<class Item> class Node { public: Node<Item>* next; Item const data; ; Node( const Item & d ) : data(d) { next = 0;

8 CHANGING GEARS: C++STL The C++ Standard Template Library is a very handy set of three built-in components: Containers: Data structures Iterators: Standard way to search containers Algorithms: These are what we ultimately use to solve problems

9 Motivation for iterators The same algorithms can be applied to multiple container classes C++ STL avoids rewriting the same algorithm for different container classes by using ITERATORS Algorithms interface with containers via iterators STL container classes array vector deque forward_list list stack queue priority_queue set multiset (non unique keys) unordered_set map unordered_map multimap bitset

10 C++ Iterators Iterators are generalized pointers. Let s consider a very simple algorithm (printing in order) applied to a very simple data structure (sorted array) 10 20 25 30 46 50 55 60 void print_inorder(int* p, int size) { for(int i=0; i<size; i++) { std::cout << *p << std::endl; ++p; We would like our print algorithm to also work with other data structures How should we modify it to print the elements of a LinkedList?

11 C++ Iterators 10 20 25 30 46 50 55 60 p Consider our implementation of LinkedList void print_inorder(linkedlist<int> * p, int size) { for(int i=0; i<size; i++) { std::cout << *p <<std::endl; ++p; When will the above code work? A. The operator << is overloaded to print the data key of a LinkedList Node B. The LinkedList class overloads the ++ operator C. Both A and B D. None of the above

C++ Iterators To solve this problem the LinkedList class has to supply to the client (print_inorder ) with a generic pointer (an iterator object) which can be used by the client to access data in the container sequentially, without exposing the underlying details of the class 12 void print_inorder(linkedlist<int>& ll) { LinkedList<int>::iterator itr = ll.begin(); LinkedList<int>::iterator en = ll.end(); while(itr!=en) { std::cout << *itr <<std::endl; ++itr; itr 10 20 25 30 curr

C++ Iterators void print_inorder(linkedlist<int>& ll) { LinkedList<int>::iterator itr = ll.begin(); LinkedList<int>::iterator en = ll.end(); What should begin() return? A. The address of the first node in the linked list container class B. An iterator type object that contains the address of the first node C. None of the above 13 while(itr!=en) { std::cout << *itr <<std::endl; ++itr; itr 10 20 25 30 curr

C++ Iterators void print_inorder(linkedlist<int>& ll) { LinkedList<int>::iterator itr = ll.begin(); LinkedList<int>::iterator en = ll.end(); while(itr!=en) { std::cout << *itr <<std::endl; ++itr; List the operators that the iterator has to implement? A. * B. ++ C.!= D. All of the above E. None of the above 14 itr 10 20 25 30 curr

C++ Iterators 15 void print_inorder(linkedlist<int>& ll) { LinkedList<int>::iterator itr = ll.begin(); LinkedList<int>::iterator en = ll.end(); while(itr!=en) { std::cout << *itr <<std::endl; ++itr; How should the diagram change as a result of the statement ++itr;? itr 10 20 25 30 curr

C++ Iterators 16 void print_inorder(linkedlist<int>& ll) { auto itr = ll.begin(); auto en = ll.end(); while(itr!=en) { std::cout << *itr <<std::endl; ++itr; How should the diagram change as a result of the statement ++itr;? itr 10 20 25 30 curr