Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can...

26
Worcester Polytechnic Institute Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) Iterators CS-2303, A-Term 2012 1

Transcript of Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can...

Page 1: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute

Iterators

Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)

Iterators CS-2303, A-Term 2012 1

Page 2: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Recall Programming Assignment #4

Read words from input

Store in binary tree

Print out words in alphabetical order, along with number of occurrences of each word

Iterators CS-2303, A-Term 2012 2

Page 3: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Recommended implementation in C++ class TreeNode { public: int incr(); int getCount(); string getWord(); int compare(const string &w2); TreeNode *setleft(TreeNode *t); TreeNode *setright(TreeNode *t); TreeNode *getleft(); TreeNode *getright();

Iterators CS-2303, A-Term 2012 3

TreeNode(const string &w); ~TreeNode(); private: const string word; int count; TreeNode *left, *right; }; // class TreeNode

TreeNode.h

Page 4: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Recommended implementation (continued)

class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree

Iterators CS-2303, A-Term 2012 4

BinaryTree.h

Page 5: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Recommended implementation (continued)

class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree

Iterators CS-2303, A-Term 2012 5

BinaryTree.h

There is something very unsatisfying about this implementation.

What is it?

Page 6: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Recommended implementation (continued)

class BinaryTree { public: TreeNode *AddNode(const string &word); int getTotalNodes(); void PrintTree(ostream &output); BinaryTree(); //Default constructor ~BinaryTree(); //Destructor private: static int totalNodes; void PrintTree(TreeNode *subtree, ostream &output); TreeNode *AddNode(TreeNode *subtree, const string &word); TreeNode *root; }; // class BinaryTree

Iterators CS-2303, A-Term 2012 7

These two functions are problem specific — not about binary trees

Output format is built into Binary Tree class!

Page 7: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Preference

Would like to make tree traversal part of Binary Tree class …

… while printing details (and other problem-specific issues) are handled outside of Binary Tree class

Iterators CS-2303, A-Term 2012 8

Page 8: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Iterator

A construct that allows cycling through objects of a data structure in some useful order…

… so program can operate on those objects in sequence

Iterators CS-2303, A-Term 2012 9

Page 9: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Characteristics of an Iterator Has state So it can keep track of where you are in the data structure

Has begin(), end(), ==, and != operations So program can start iterator, compare iterator objects, and know

when it has finished iterating

Has ++ operator (and possibly -- operator) So program can step to next object (and possibly previous object)

May have [], *, -> operators So program can access specific objects from iterator

Iterators CS-2303, A-Term 2012 10

Page 10: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Binary tree iterators

Pre-order

Post-order

In-order

Iterators CS-2303, A-Term 2012 11

Page 11: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Binary tree in-order iterator

/* This iterator uses a stack object from Standard Template Library*/

#include <stack> class iterator { TreeNode *node; // pointer to current iteration

stack<TreeNode *> stack; // record of how we got there

public: iterator(); //constructor TreeNode *begin(); TreeNode *end(); TreeNode *operator++(); bool operator==(iterator &); bool operator!=(iterator &); } //class iterator

Iterators CS-2303, A-Term 2012 12

Page 12: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Binary tree in-order iterator (continued)

iterator::iterator() { stack.push(NULL); // initialize stack

node = root; if (node != NULL) while (node->left) { stack.push(node); node = node->left; }

return; }

iterator::operator++(){ current = node; if (node -> right) { node = node -> right; while (node -> left) { stack.push(node); node = node-> left; } } else node = stack.pop();

return current; }

Iterators CS-2303, A-Term 2012 13

Page 13: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Binary tree in-order iterator (continued)

/* This iterator uses a stack object from Standard Template Library*/

#include <stack> class iterator { TreeNode *node; // pointer to current iteration

stack <TreeNode *> stack; // record of how we got there

public: iterator(); //constructor TreeNode *begin(); TreeNode *end(); TreeNode *operator++(); bool operator==(iterator &); bool operator!=(iterator &);

} //class iterator

Iterators CS-2303, A-Term 2012 14

Page 14: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Binary tree in-order iterator (continued)

/* This iterator uses a stack object from Standard Template Library*/

#include <stack> class iterator { TreeNode *node; // pointer to current iteration

stack <TreeNode *> stack; // record of how we got there

public: iterator(); //constructor TreeNode *begin(); TreeNode *end(); TreeNode *operator++(); bool operator==(iterator &); bool operator!=(iterator &);

} //class iterator

Iterators CS-2303, A-Term 2012 15

For these methods to work, class iterator must be a friend of class TreeNode

Private members of TreeNode must become protected

Page 15: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Binary tree in-order iterator (continued)

iterator::TreeNode *begin(){ while (!stack.empty()) stack.pop(); stack.push(NULL); // re-initialize stack node = root; if (node != NULL) while (node->left) { stack.push(node); node = node->left; } return node; }

iterator::TreeNode *end(){ return NULL } //iterator::TreeNode *end bool iterator::operator==(iterator

it &){ return node == it.node; } //iterator::TreeNode *end bool iterator::operator!=(iterator

it &){ return node != it.node; } //bool iterator::operator!=

Iterators CS-2303, A-Term 2012 16

Page 16: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Binary tree in-order iterator (concluded)

An iterator that steps through the binary tree in the appropriate order

Needs access to protected members of the TreeNode class

Pre-order and post-order traversal are easy and obvious extensions

Allows the design of a Binary Tree independent of what we want to do with it.

Iterators CS-2303, A-Term 2012 17

Page 17: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute

Questions?

Iterators CS-2303, A-Term 2012 18

Page 18: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Reading

Absolute C++, §17.3 & 19.1

Iterators also exist in Java Similar in concept Different in detail

Iterators CS-2303, A-Term 2012 19

Page 19: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Iterator – An Abstraction

Generalization of pointer into an array

Key concepts Access to current element:– * and −>

(Sometimes also random access:– [ ] ) Next element:– ++

(Sometimes also previous element:– -- ) Equality:– == and !=

Iterators CS-2303, A-Term 2012 20

Page 20: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Iterator Example list<Event> EventQueue; list<Event>::iterator it; for (it=EventQueue.begin(); it != EventQueue.end(); it++) { if (it −> time > newEvent.time) EventQueue.insert(it, newEvent); break; } // for (it = ...)

Iterators CS-2303, A-Term 2012 21

Iterator acts like a pointer begin() method set “pointer” to first element

end() method points to one after last element!

Page 21: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

More on Iterators

Can build own iterators for own container classes

Different iterators provide different operations E.g., linked list does not offer "--" or “[ ]" string and vector containers do offer "--" or “[ ]"

Iterators CS-2303, A-Term 2012 22

Page 22: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Uses of Iterators

To help you sequence through objects of a container — e.g.,

Arrays, vectors Lists (singly- doubly-linked) Trees Input Output …

Iterators CS-2303, A-Term 2012 23

Page 23: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Iterator Example (again)

list<Event> EventQueue; list<Event>::iterator it; for (it=EventQueue.begin(); it != EventQueue.end(); it++) { if (it −> time > newEvent.time) EventQueue.insert(it, newEvent); break; } // for (it = ...)

Iterators CS-2303, A-Term 2012 24

In this case, it appears that iterator is a member class or

struct of class list<class T>

Page 24: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

The following is legal in C++

class C { class D { … } // class C } // class C

Class D is a nested class in Class C aka “local class”

Member functions of class D can access members of class C

Class D may be public, protected, private

See Absolute C++ §7.2 (end)

Iterators CS-2303, A-Term 2012 25

You can also declare classes inside of functions!

Page 25: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute Worcester Polytechnic Institute

Recall Programming Assignment #4

Read words from input

Store in binary tree

Print out words in alphabetical order, along with number of occurrences of each word

Iterators CS-2303, A-Term 2012 26

What other iterator is suggested by this problem?

Page 26: Iterators - web.cs.wpi.eduweb.cs.wpi.edu/.../Lectures_A12/Week6_Iterators.pdf · So program can start iterator, compare iterator objects, and know when it has finished iterating Has

Carnegie Mellon Worcester Polytechnic Institute

CS-2303, A-Term 2012 Iterators 27

Questions?