Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

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

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

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

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

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

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

Computer Science II CSci 1200 Sections 6-10 Week 3, Monday Lecture January 28, 2002 Functions, Structs and Multiple Files

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

Computer Science II Lecture 1 Introduction and Background

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

Introduction to Computer Science Midterm 3 Fall, Points

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

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

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

CSCI-1200 Data Structures Spring 2018 Lecture 15 Associative Containers (Maps), Part 2

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

Sets: Efficient Searching and Insertion

Data Structures CSci 1200 Test 1 Questions

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

PIC 10A. Lecture 23: Intro to STL containers

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

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

Templates and Vectors

DELHI PUBLIC SCHOOL TAPI

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Data Structures CSci 1200 Test 2 Questions

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

C++ for Java Programmers

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

Increment and the While. Class 15

a data type is Types

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

CS31 Discussion 1E Spring 17 : week 08

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Data Structures and Algorithms

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

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Computer Science II Lecture 2 Strings, Vectors and Recursion

CS 142 Style Guide Grading and Details

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

15. Pointers, Algorithms, Iterators and Containers II

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

by Pearson Education, Inc. All Rights Reserved.

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

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CSCI-1200 Data Structures Fall 2016 Lecture 4 Classes II: Sort, Non-member Operators

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

Data types. CISC 1600/1610 Computer Science I. Array syntax. Memory allocation. Zero-indexing 4/4/2016. Arrays

CSCI-1200 Data Structures Fall 2017 Lecture 4 Classes II: Sort, Non-member Operators

Computer Programming

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Sixth lecture; classes, objects, reference operator.

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

Computer Programming : C++

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

Computer Science II CSci 1200 Test 1 Overview and Practice

Scientific Computing

Review of Important Topics in CS1600. Functions Arrays C-strings

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

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Data Structures Lecture 3 Order Notation and Recursion

C++ Arrays and Vectors

C++ Programming Chapter 7 Pointers

Topic 8: Lazy Evaluation

Computer Science II CSci 1200 Test 2 Overview and Practice

PRACTICE MIDTERM EXAM #2

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

BITG 1113: Function (Part 2) LECTURE 5

CE221 Programming in C++ Part 1 Introduction

CS11 Advanced C++ Spring 2018 Lecture 2

Computer Programming

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Lab 4X - Intro to MOOS Programming HINTS

Working with Batches of Data

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

PIC 10A Objects/Classes

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

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

14. Pointers, Algorithms, Iterators and Containers II

CPSC 427a: Object-Oriented Programming

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

STUDENT LESSON A15 ArrayList

Lecture on pointers, references, and arrays and vectors

BITG 1113: POINTER LECTURE 12

W3101: Programming Languages C++ Ramana Isukapalli

7 TEMPLATES AND STL. 7.1 Function Templates

List Iterator Implementation

Programming for Engineers Arrays

Transcription:

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples Test 1 102 total points on the test. Therefore, your score is out of 102. Class average: 89.6 / 102 Distribution: Solutions are posted on line Reminder Range Percent Count 92-102 90-100 62 81.5-91.5 80-89 40 71.5-81 70-79 18 61-71 60-69 10 50.5-61.5 50-59 10 50 50 9 If you did not receive credit for Checkpoint 3 in last week s lab (Lab 4), you may show it to a TA at the start of lab this week in order to earn credit. This applies to last week s lab only. Review from Lecture 7 Koenig & Moo: Chapter 5.1-5.5 We wrote several versions of a program to maintain a class enrollment list and an associated waiting list. The first version used vectors to store the information. Unfortunately, erasing items from vectors is inefficient. In the second version, we explored iterators and iterator operations as a different means of manipulating the contents of the vector. This allows us to replace the vector with a list in the third version. We will review this solution today.

Of particular note, there is an erase function for both vectors and lists. The vector erase function does pretty much what we did in our enrollment example program. The list erase function is mush more efficient. As we will see, for the enrollment problem the list is a better sequential container class than the vector. Today s Class Review; Iterators; Lists; Programming Examples Returning references to member variables from member functions Lists Review of iterators and iterator operations Differences between indices and iterators Differences between lists and vectors Prime number programming example 2

References and Return Values There is one part of the Student class that we did not discuss thoroughly in Lecture 6 and I d like to discuss it now. The topic is returning references to objects. Here is an excerpt from the code from Lecture 6. class Student { public: bool read( istream& in_str, unsigned int num_homeworks, unsigned int num_tests ); void compute_averages( double hw_weight ); const string& first_name() const { return first_name_; } const string& last_name() const { return last_name_; } // etc... private: string first_name_; string last_name_; string id_number_; // etc... }; A reference is an alias for another variable. For example string one = "Tommy"; string& two = one; // two is a reference to one two[1] = i ; cout << one << endl; // This outputs the string Timmy The reference variable two refers to the same string as variable one. Therefore, when we change two, we are changing one. Exactly the same thing occurs with reference parameters to functions. Returning to the student grades program, in the main function we had a vector of students: vector<student> students; 3

Based on our discussion of references and looking at the class declaration above, the question arises, what if we wrote: string & fname = students[i].first_name(); fname[1] = i Would the code then be changing the internal contents of the i-th Student object? The answer is NO! The reason is that the Student class function first_name returns a const reference. The compiler will complain that the above code is attempting to assign a const reference to a non-const reference variable. If you instead wrote: const string & fname = students[i].first_name(); fname[1] = i The compiler would complain that you are trying to change a const object. Hence in both cases you d be safe. HOWEVER, you d get yourself in trouble if the member function return type was only a reference, and not a const reference. Then you could access and change the internal contents of an object! This is a bad idea in most cases. 4

Lists Our second standard-library container class Lists are formed as a sequentially linked structure instead of the arraylike, random-access / indexing structure of vectors. Pictures showing the differences will be drawn in class. Lists have push front and pop front functions in addition to the push back and pop back functions of vector Erasing is very efficient for a list this is independent of the size of the list. We can t use the standard sort function; we must use a special sort function defined by the list type. Lists have no subscripting operation. 5

Iterators and Iterator Operations General An iterator type is defined by each container class. For example, vector<double>::iterator p; list<string>::iterator q; There are also string iterators, which work just like vector iterators. An iterator is assigned to a specific location in a container. For example, p = v.begin() + i; // i-th location in the vector q = r.begin(); // first entry in the list when v is a vector of doubles and r is a list of strings. The contents of the specific entry referred to by an iterator are accessed using the * operator. For example, *p = 3.14; cout << *q << endl; *q = "Hello"; In the first and third lines, *p and q are l-values. In the second, *q is an r-value. Stepping through a container, either forward and backward, is done using increment (++) and decrement (--). Hence, ++ p ; q -- ; moves p to the next location in the vector and moves q to the previous location in the list. Neither operation changes any of the contents of vector v or list r! Finally, we can change the container that an iterator p attached to as long as the types are correct. Thus, if v and w are both vector<double>, then 6

p = v.begin(); *p = 3.14; // changes 1st entry in v p = w.begin() + 2; *p = 2.78; // changes 3rd entry in w are all valid because p is a vector<double>::iterator, but if a is a vector<string> then p = a.begin(); is a syntax error because of a type clash! Iterators and Iterator Operations Vector Iterators Vector (and string) iterators have special capabilities that other container iterators (list for now, but others later) do not have Initialization at a random spot in the vector: p = v.begin() + i; Jumping around inside the vector through addition and subtraction of location counts: p = p + 5; moves p 5 locations further in the vector. Neither of these is allowed for list iterators (and most other iterators, for that matter) because of the way containers are built. Iterators vs. Indices for Vectors and Strings Students are often confused by the difference between iterators and indices for vectors. Consider the following declarations: vector<double> a(10, 2.5), b(20, 1.1); vector<string> c(3, string("hi")); vector<double>::iterator p = a.begin() + 5; unsigned int i=5; 7

Iterator p refers to location 5 in vector a. The value stored there is directly accessed through the * operator: *p = 6.0; This has changed the contents of vector a. We can also access the contents of vector a using subscripting. We will treat i as the subscript. It is not attached to vector a in any way. We write, cout << a[i] << endl; to access and output the value stored at location 5 of a. Lists vs. Vectors Erase Lists are a chain of separate memory blocks, one block for each entry. Vectors are formed as a contiguous (and bigger) block of memory. Lists therefore allow easy/fast insert and remove in the middle, but not indexing. Vectors therefore allow indexing (which depends on jumping around inside the block of memory), but slow insert and remove in the middle. Lists and vectors each have a special member function called erase. In particular, given list of ints s, consider the example list<int>::iterator p = s.begin(); ++p; list<int>::iterator q = s.erase(p); After the function erase, The integer stored in the second entry of the list has been removed. The size of the list has shrunk by one. 8

The iterator p does not refer to a valid entry. The iterator q refers to the item that was the third entry and is now the second. You will often see code like the above written list<int>::iterator p = s.begin(); ++p; p = s.erase(p); This makes iterator p refer to a valid entry. Our erase from vector function from the Lecture 7 enrollment example becomes simply p = v.erase(p); Even though this has the same syntax for vectors and for list, the vector version is O(n), whereas the list version is O(1). Prime Numbers: Sieve of Eratosthenes We will explore the problem of finding all primes less than a given integer, n, and introduce the Sieve of Eratosthenes algorithm. The algorithm is a casting out algorithm: each new prime is used to cast out all of its multiples from a list of potential primes. We will implement this during lecture using the skeleton program provided on the next page of this handout. Central to the code will be the use of iterators. 9