Structuur van Computerprogramma s 2

Similar documents
Structuur van Computerprogramma s 2

Structuur van Computerprogramma s 2

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

CSE 100: C++ TEMPLATES AND ITERATORS

CSE 100: C++ TEMPLATES AND ITERATORS

Structuur van Computerprogramma s 2

CS11 Advanced C++ Fall Lecture 1

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Dynamic Data Structures

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Faculty of Information and Communication Technologies

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

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

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

CSI33 Data Structures

G52CPP C++ Programming Lecture 18

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

CSCI-1200 Data Structures Fall 2018 Lecture 21 Hash Tables, part 1

CSE 100: C++ TEMPLATES AND ITERATORS

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

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

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

SETS AND MAPS. Chapter 9

List, Stack, and Queues

Intermediate Programming, Spring 2017*

Abstract Data Types 1

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

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

STL: C++ Standard Library

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CSCI-1200 Data Structures Fall 2009 Lecture 20 Hash Tables, Part II

Programming with Haiku

Abstract Data Types 1

CS 216 Exam 1 Fall SOLUTION

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

1. The term STL stands for?

CS11 Advanced C++ Spring 2018 Lecture 2

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

Data Structures and Algorithms

TEMPLATES AND ITERATORS

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

Unit 4 Basic Collections

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

Standard Template Library. Containers, Iterators, Algorithms. Sequence Containers. Containers

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Traversing Trees with Iterators

Traversing Trees with Iterators

CS

COEN244: Class & function templates

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

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

The Standard Template Library. An introduction

Lecture 15a Persistent Memory & Shared Pointers

CSCI-1200 Data Structures Test 3 Practice Problem Solutions

CS24 Week 4 Lecture 2

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

CS197c: Programming in C++

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

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Standard Library Reference

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

SFU CMPT Topic: Function Objects

The Standard Template Library Classes

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

EL2310 Scientific Programming

Motivation for Templates

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

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Predictive Engineering and Computational Sciences. Data Structures and Generic Programming. Roy H. Stogner. The University of Texas at Austin

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

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

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

Objects Managing a Resource

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

CSCI 104 Templates. Mark Redekopp David Kempe

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

CSCI-1200 Data Structures Fall 2015 Lecture 24 Hash Tables

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

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

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

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

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

Data Structures and Algorithms

Questions: ECE551 PRACTICE Final

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

Hashing. mapping data to tables hashing integers and strings. aclasshash_table inserting and locating strings. inserting and locating strings

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

CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

Arrays and Linked Lists

Object Oriented Programming in C++

EL2310 Scientific Programming

Structuur van Computerprogramma s 2

Chapter 5. The Standard Template Library.

Hashing. inserting and locating strings. MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010.

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

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

Transcription:

Structuur van Computerprogramma s 2 dr. Dirk Deridder Dirk.Deridder@vub.ac.be http://soft.vub.ac.be/ Vrije Universiteit Brussel - Faculty of Science and Bio-Engineering Sciences - Computer Science Department - Pleinlaan 2-1050 Brussels - Belgium

Chapter 6 - Generic Programming using the STL Structuur van Computerprogramma s 2

Motivation

Sequential Search in an Array (find in subarray) (find in full array) // sequential search for value in [*first..*last[; // return last if not found template<typename T> const T* find(const T* first, const T* last, const T& value) { while (first!= last && *first!= value) ++first; return first; extern int a[size]; // find 20 in array a find(a, a+size, 20); // return pointer to 20 or a+size 4

Sequential Search in a Linked List // Node<T>* is a (too) simple linked list of T template<typename T> class Node { public: // constructor Node(const T& val, Node* cdr = 0) : value_(val), cdr_(cdr) { // cons creates a node (value, next) as in Scheme (i.e. prepend) Node* cons(const T& val) { return new Node(val, this); // car returns first slot of a node (value) const T& car() const { return value_; // cdr returns second slot of a node (next) Node* cdr() const { return cdr_; private: T value_; Node* cdr_; ; value 5

Sequential Search in a Linked List (find in sub-linked-list) (provided as a parameter) (find until null pointer of type Node) 6

Sequential Search in a Linked List template<typename T> Node<T>* // search for value in linked list [first.. last] find(node<t>* first, Node<T>* last, const T& value) { while (first!= last && first->car()!= value) first = first->cdr(); return first; extern Node<int> *list; // a pointer to a Node with int values // find 20 in linked list from start until null pointer find(list, static_cast<node<int>*>(0), 20); 7

The Essence of Sequential Search in a Container Advance a Cursor in the Container until either the value is found, or all elements of the container have been checked Cursor find(cursor start, Cursor end, T value) { while ( start!=end && (object_pointed_to_by_start!= value) ) advance start to next position in container; return start; 8

Requirements on Cursor class Cursor { // + constructors still to be added //... public: // DEREFERENCE cursor to obtain an object // from the container T operator*(); // RETURN cursor that refers to the NEXT // object in the container Cursor operator++(); // COMPARE two cursors bool operator!=(cursor); // ASSIGN a cursor to another one (not really required by find) Cursor& operator=(const Cursor&); ; // sequential search Cursor find(cursor start, Cursor end, T value) { while (start!= end && (*start!= value)) ++start; return start; 9

A Generic find Function A generic algorithm is pure, i.e. independent of the data types on which it operates template <typename InputIterator, typename T> InputIterator find(inputiterator first, InputIterator last, const T& value) while (first!= last && *first!= value) ++first; return first; This works for arrays, InputIterator is T* for linked lists, we must implement a Node<T>::Cursor class that implements the requirements 10

Linked List Iterator template<typename T> class Node { public: //... // Cursor that satisfies find requirements class Cursor { public: Cursor(Node* node = 0) : node_(node) { // Dereference const T& operator*() const { return node_->car(); // Return next Cursor& operator++() { node_ = node_->cdr(); return *this; // Compare bool operator!=(const Cursor& c) const { return node_!= c.node_; private: Node* node_; ; ; 11

Linked List Iterator Example extern Node<int> *list; find(node<int>::cursor(list), Node<int>::Cursor(), 20); // start, end, value Improve the readability by defining: template<typename T> class Node { public: //... class Cursor { //... ; // return Cursor pointing to the start of the list Cursor begin() { return Cursor(this); // return Cursor pointing past the end of the list Cursor end() { return Cursor(); ; then find(list->begin(), list->end(), 20) also works 12

Containers and Algorithms The STL provides many Container Class Templates: Sequences: vector, list, dequeue Associative: set, multiset, map, multimap, hash table Adapters: stack, queue, priority queue It also supports more than 50 algorithms that use these containers: Non-mutating sequence operations: e.g. find, find_if, for_each,... Mutating sequence operations: e.g. copy, replace, erase,... Sorting operations... 13

Why Iterators? The goal is to write algorithms that work for many containers To implement each algorithm for each container would require hundreds of implementations This is possible by putting a new abstraction between algorithms and containers (data structures) : iterators An iterator is like an abstract pointer: it may support dereference (using operator*()): *it increment, decrement: ++it, --it, it++, it-- random access: it+5 14

A Container and its Iterator template<typename Element> class C { CIterator begin(); // "pointer" to first element in C CIterator end(); // "pointer" beyond last element in C ; template<typename Element> class CIterator { public: Element operator*(); // dereference to get value of an element CIterator operator++(); // increment to get next container element ; 15

A Simple Algorithm using Containers // works for any iterator on any container template<typename Iterator, typename T> // return iterator pointing to cell containing value or last Iterator find(iterator first, Iterator last, const T& value) { while (first!= last && *first!= value) ++first; return first; C<std::string> box; std::string s("abc"); CIterator it; if ( (it=find(box.begin(), box.end(), s)) == box.end()) cout << s << " not found" << endl; else cout << s << " found: " << *it << endl; 16