21+-+Iterators - Overview EECS 280 Programming and...

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

View Full Document Right Arrow Icon
11/30/2009 1 Iterators EECS 280 Programming and Introductory Data Structures 1 Overview 2 c So far, we've looked at several different kinds of abstractions: 1. Functional generalization 2. Abstract data types 3. Containers c Today, we'll talk about what happens when containers and functional abstraction meet. Overview 3 c Several weeks ago, we talked about a collection of abstract data types: c IntSet c a subtype of an IntSet called a MaxIntSet c MaxInstSet was just like an IntSet , with one additional method: int max(); // REQUIRES: set is non-empty // EFFECTS: returns the largest element in set. c max walked down the set of integers, computing the maximum. Iterators Introduction 4 c While max works, it is unsatisfactory since there are many computations that could be performed on a set of integers. c 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. c The second is preferable and is called an "iterative abstraction", or commonly an "iterator" because it allows clients to iterate over the members of some container. c In principle, any container class could support an iterator. Iterators Introduction 5 c 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. c So, iterators are a separate abstract data type. c Usually, for each container type one has, there is also (at least) one iterator type defined. Iterators Details 6 c An iterator has the following operations: 1. create : create a new iterator, bound to some particular instance of a container class. c Once an iterator is created, the underlying container cannot be modified in any way, else the iterator is invalid. c 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. c In general, an iterator makes no guarantee about the order in which objects are returned, though it is possible to define specific iterators with such a guarantee (like when working with sorted lists).
Background image of page 1

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

View Full DocumentRight Arrow Icon
11/30/2009 2 Iterators Details 7 c An iterator has the following operations: 2. next : return an object from the container. c
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/26/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

Page1 / 5

21+-+Iterators - Overview EECS 280 Programming and...

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

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