Intermediate Programming, Spring 2017*

Similar documents
Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

Linked List using a Sentinel

Intermediate Programming, Spring 2017*

Templates and Vectors

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017*

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Program template-smart-pointers-again.cc

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

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

Program template-smart-pointers.cc

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

Dynamic Data Structures

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Fast Introduction to Object Oriented Programming and C++

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

STL: C++ Standard Library

Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const.

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

CSI33 Data Structures

Short Notes of CS201

Introduction to Programming

1 Short Answer (5 Points Each)

Advanced Systems Programming

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

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness.

CS201 - Introduction to Programming Glossary By

Review Questions for Final Exam

CSCE 110 PROGRAMMING FUNDAMENTALS

Intermediate Programming, Spring 2017*

Lecture on pointers, references, and arrays and vectors

CSCI 104 Templates. Mark Redekopp David Kempe

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Exceptions, Case Study-Exception handling in C++.

Due Date: See Blackboard

Class string and String Stream Processing Pearson Education, Inc. All rights reserved.

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

pointers + memory double x; string a; int x; main overhead int y; main overhead

Object Oriented Design

CS

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

pointers & references

Function Overloading

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

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

Week 3: Pointers (Part 2)

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

CE221 Programming in C++ Part 1 Introduction

lecture09: Linked Lists

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Lecture 7. Log into Linux New documents posted to course webpage

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

W3101: Programming Languages C++ Ramana Isukapalli

Doubly-Linked Lists

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

G52CPP C++ Programming Lecture 18

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Lecture 23: Pointer Arithmetic

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

CS 247: Software Engineering Principles. ADT Design

Due Date: See Blackboard

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CSCE 110 PROGRAMMING FUNDAMENTALS

C++ For Science and Engineering Lecture 12

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Solution to CSE 250 Final Exam

CS 61B, Spring 1996 Midterm #1 Professor M. Clancy

Introducing C++ to Java Programmers

CMSC 202 Midterm Exam 1 Fall 2015

Unit 7. 'while' Loops

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Lab 2: ADT Design & Implementation

Intermediate Programming, Spring 2017*

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Motivation for Templates

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

Consider the program...

The University of Nottingham

C++_ MARKS 40 MIN

Introduction to Core C++

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

boost::enable_if Deep Down Boostimagical Fun Christian Bay and Kasper A Andersen Department of Computer Science University of Copenhagen

COEN244: Class & function templates

CSC1322 Object-Oriented Programming Concepts

Constructors.

Intermediate Programming, Spring 2017*

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CPSC 427a: Object-Oriented Programming

EECE.3220: Data Structures Spring 2017

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Tokens, Expressions and Control Structures

Lesson 13 - Vectors Dynamic Data Storage

Transcription:

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.

Outline Iterators

Iterators In our code, we often work with lists of values: vec.h class Vec T* _values; size_t _size; Vec( int size ) : _size(size) _values = new T[_size]; ~Vec( void ) delete[] _values; size_t size( void ) const return _size; T& operator[] ( size_t i ) return _values[i]; const T& operator[] ( size_t i ) const return _values[i]; #include <iostream> #include "vec.h" using namespace std; >>./a.out 0 main.cpp 3 5 >> void Print( const Vec< int >& v ) for( size_t i=0 ; i<v.size() ; i++ ) cout << v[i] << endl; int main( void ) Vec< int > v( 3 ); v[0] = 0, v[1] = 3, v[2] = 5; Print( v ); return 0;

Iterators In our code, we often work with lists of values: list.h class List List< T >* next; T value; List ( T v, List< T >* n=null ) : value(v), next(n) Note: The constructor performs default initialization if the second argument is not provided. (Can only provide default values for the last arguments.) #include <iostream> #include "list.h" using namespace std; >>./a.out 0 main.cpp 3 5 >> void Print( const List< int >& l ) for( const List< int >* i=&l ; i!=null ; i=i->next ) cout << i->value << endl; int main( void ) List< int > l1( 0 ), l2( 3 ), l3( 5 ); l1.next = &l2, l2.next = &l3; Print( l1 ); return 0;

Iterators When working with lists of objects, we don't want to special-case the type-specific ways for running through the elements of the list #include <iostream> #include "vec.h" using namespace std; main.cpp #include <iostream> #include "list.h" using namespace std; main.cpp void Print( const Vec< int >& v ) for( size_t i=0 ; i<v.size() ; i++ ) cout << v[i] << endl; void Print( const List< int >& l ) for( const List< int >* i=&l ; i!=null ; i=i->next ) cout << i->value << endl;

Iterators In our code, we often work with lists of values: We unify the iteration by defining an auxiliary "pointer-like" object / iterator for walking through the list We need to: Get an iterator that "points" to the beginning of the list Get an iterator that "points" just past the end of the list Be able to dereference the iterator Be able to advance the iterator Be able to check if two iterators are equal main.cpp template< typename Container > void Print( const Container& c ) for( PointerLikeObject p=c.begin() ; p!=c.end() ; ++ p ) cout << *p << endl;

Iterators In C++, when we have a container class, we define the iterator as a public nested class called: container.h iterator if we want to be able to modify the values of the reference const_iterator if we do not class Container class iterator

Iterators In C++, when we have a container class, we define the iterator as a public nested class called: container.h The iterator must overload: The reference operator The increment operator The inequality operator class Container class iterator T& operator * ( ); iterator& operator ++ (); bool operator!= ( const iterator& i ) const;

Iterators In C++, when we have a container class, we define the iterator as a public nested class called: container.h The iterator must overload: The reference operator The increment operator The inequality operator The container must define: A begin method An end method class Container class iterator iterator begin( void ); iterator end( void ); const_iterator begin( void ) const; const_iterator end( void ) const;

Iterators Putting these together, we can define generic code: main.cpp template< typename C > void Print( const C& c ) for( typename C::const_iterator i=c.begin() ; p!=c.end() ; ++i ) cout << *i << endl; Note: The keyword typename is needed to let the compiler know that const_iterator is a class / type, not a static member. class Container class iterator container.h iterator begin( void ); iterator end( void ); const_iterator begin( void ) const; const_iterator end( void ) const;

Iterators: Vec vec.h class Vec T* _values; size_t _size; Vec( int size ); ~Vec( void ); size_t size( void ) const; T& operator[] ( size_t i ); const T& operator[] ( size_t i ) const; const T* _ptr; const_iterator( const T* ptr ) : _ptr( ptr )

Iterators: Vec dereference vec.h class Vec T* _values; size_t _size; Vec( int size ); ~Vec( void ); size_t size( void ) const; T& operator[] ( size_t i ); const T& operator[] ( size_t i ) const; const T* _ptr; const_iterator( const T* ptr ) : _ptr( ptr ) const T& operator * ( ) const return *_ptr;

Iterators: Vec pre-increment vec.h class Vec T* _values; size_t _size; Vec( int size ); ~Vec( void ); size_t size( void ) const; T& operator[] ( size_t i ); const T& operator[] ( size_t i ) const; const T* _ptr; const_iterator( const T* ptr ) : _ptr( ptr ) const T& operator * ( ) const return *_ptr; const_iterator& operator ++ () _ptr++ ; return *this;

Iterators: Vec inequality vec.h class Vec T* _values; size_t _size; Vec( int size ); ~Vec( void ); size_t size( void ) const; T& operator[] ( size_t i ); const T& operator[] ( size_t i ) const; const T* _ptr; const_iterator( const T* ptr ) : _ptr( ptr ) const T& operator * ( ) const return *_ptr; const_iterator& operator ++ () _ptr++ ; return *this; bool operator!= ( const const_iterator& i ) const return _ptr!=i._ptr;

Iterators: Vec beginning / ending iterators vec.h class Vec T* _values; size_t _size; Vec( int size ); ~Vec( void ); size_t size( void ) const; T& operator[] ( size_t i ); const T& operator[] ( size_t i ) const; const T* _ptr; const_iterator( const T* ptr ) : _ptr( ptr ) const T& operator * ( ) const return *_ptr; const_iterator& operator ++ () _ptr++ ; return *this; bool operator!= ( const const_iterator& i ) const return _ptr!=i._ptr; const_iterator begin( void ) const return const_iterator( _values ); const_iterator end( void ) const return const_iterator( _values+_size );

Iterators: List list.h class List List< T >* next; T value; List ( T v, List< T >* n=null ); const List< T >* _ptr; const_iterator( const List< T >* ptr ) : _ptr( ptr )

Iterators: List dereference list.h class List List< T >* next; T value; List ( T v, List< T >* n=null ); const List< T >* _ptr; const_iterator( const List< T >* ptr ) : _ptr( ptr ) const T& operator * ( ) const return _ptr->value;

Iterators: List pre-increment list.h class List List< T >* next; T value; List ( T v, List< T >* n=null ); const List< T >* _ptr; const_iterator( const List< T >* ptr ) : _ptr( ptr ) const T& operator * ( ) const return _ptr->value; const_iterator& operator ++ () _ptr=_ptr->next ; return *this;

Iterators: List inequality list.h class List List< T >* next; T value; List ( T v, List< T >* n=null ); const List< T >* _ptr; const_iterator( const List< T >* ptr ) : _ptr( ptr ) const T& operator * ( ) const return _ptr->value; const_iterator& operator ++ () _ptr=_ptr->next ; return *this; bool operator!= ( const const_iterator& i ) const return _ptr!=i._ptr;

Iterators: List beginning / ending iterators list.h class List List< T >* next; T value; List ( T v, List< T >* n=null ); const List< T >* _ptr; const_iterator( const List< T >* ptr ) : _ptr( ptr ) const T& operator * ( ) const return _ptr->value; const_iterator& operator ++ () _ptr=_ptr->next ; return *this; bool operator!= ( const const_iterator& i ) const return _ptr!=i._ptr; const_iterator begin( void ) const return const_iterator( this ); const_iterator end( void ) const return const_iterator( NULL );

Iterators: Vec When the iterator is a pointer, things can be made simpler vec.h class Vec T* _values; size_t _size; Vec( int size ); ~Vec( void ); size_t size( void ) const; T& operator[] ( size_t i ); const T& operator[] ( size_t i ) const; const T* _ptr; const_iterator( const T* ptr ) : _ptr( ptr ) const T& operator * ( ) const return *_ptr; const_iterator& operator ++ () _ptr++ ; return *this; bool operator!= ( const const_iterator& i ) const return _ptr!=i._ptr; const_iterator begin( void ) const return const_iterator( _values ); const_iterator end( void ) const return const_iterator( _values+_size );

Iterators: Vec When the iterator is a pointer, things can be made simpler vec.h class Vec const T* _ptr; T* _values; const_iterator( const T* ptr ) : _ptr( ptr ) size_t _size; const T& operator * ( ) const return *_ptr; const_iterator& operator ++ () _ptr++ ; return *this; Vec( int size ); bool operator!= ( const const_iterator& i ) const ~Vec( void ); size_t size( void ) const; return _ptr!=i._ptr; T& operator[] ( size_t i ); const T& operator[] ( size_t i ) const; const_iterator begin( void ) const return const_iterator( _values ); typedef const T* const_iterator; const_iterator end( void ) const return const_iterator( _values+_size ); const_iterator begin( void ) const return _values; const_iterator end( void ) const return _values+_size;

Piazza Resources section Resources tab Exercise 13-2