23+-+Iterators - EECS 280 Programming and Introductory Data...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Iterators EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overview 2 So far, we've looked at several different kinds of abstractions: 1. Functional generalization 2. Abstract data types 3. Containers Today, we'll talk about what happens when containers and functional abstraction meet.
Background image of page 2
Overview 3 Several weeks ago, we talked about a collection of abstract data types: IntSet a subtype of an IntSet called a MaxIntSet MaxInstSet was just like an IntSet , with one additional method: int max(); // REQUIRES: set is non-empty // EFFECTS: returns the largest
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Iterators Introduction 4 While max works, it is unsatisfactory since there are many computations that could be performed on a set of integers. As ADT designers, we have at least two choices: 1. Predict all of a client's needs, and provide a method for each of them. (Sum, min, max, . ..) 2. ***Provide an abstraction that can access each object in the set, with exactly-once semantics.
Background image of page 4
Iterators Introduction 5 An iterator is a separate abstraction from the container itself. There are two reasons why this is so: 1. An iterator's lifetime is shorter than its container's: the container must exist before you iterate over its contents, and the container can't be destroyed safely before the iterator is. 2. A container can have more than one "live" iterator at a time. So, iterators are a separate abstract data
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Iterators Details 6 An iterator has the following operations: 1. create : create a new iterator, bound to some particular instance of a container class. Once an iterator is created, the underlying container cannot be modified in any way, else the iterator is invalid. The intuition behind this is that the iterator depends on the representation of the container – if that changes, the iterator is likely to miss an element or return an element that no longer exists.
Background image of page 6
Iterators Details 7 An iterator has the following operations: 2. next : return an object from the container. Each object will be returned exactly once, provided the iterator is not made invalid. If all objects have previously been returned,
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

23+-+Iterators - EECS 280 Programming and Introductory Data...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online