TITLE: of what use iterators, you say PROBLEM: jemenake@trumpet.aix.calpoly.edu (Joseph A. Emenaker) ... However, back when I wrote a linked-list class of my own once, I chose to have some methods that would allow me to do things like going to the beginning of the list, going to the end, going to the next, pervious, etc. This allowed me to do some reasonably intuitive looking for-loops like so: SomeClass *current; MyList example(AnotherListWithSomeDataInItAlready); for(current=example.GetFirst(); current!=NULL; current=example.GetNext()) { blah blah blah } However, since it doesn't take a stroke of genious to think up something like this, I'm sure it's been thought of before. Thus, it must have been deliberately discarded in favor of the "let's have a whole separate class for our iterator" concept. So, my question is: why? RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 14 Jan 95 I assume you must have put a cursor in the List class to keep track of current position. Otherwise, how would you figure out what the "next" item was? Now suppose I need to have two list walks active at the same time (for example, quicksort). Your list can't be used for that purpose. OK, lets put in two cursors, c1 and c2. If c1 is in use, use c2. Hmmm. A little tricky to coordinate use of the cursors from different functions, but let's set that aside. What if I need 3 (or 4 or 5) cursors simultaneously active? OK, it's unlikely that more than 10 cursors would be needed, so we'll put in an array of 10 cursors and use a cursor index to access them. This design is collapsing under its own weight. Consider a C-style array: double data[max]; How do we walk through the array? We declare an auxiliary variable: for( int index=0; index < max; ++index ) { ... } If we need to have multiple cursors active, we declare new index variables as needed. No user of the array needs to know about any other user walking the array. What is the "index" variable? It is an iterator of a type which knows how to keep track of the current position in the array and how to advance to the next element. Iterator classes generalize this concept. The iterator class knows how to keep track of a position in a container, how to find the "next" item, and whether there are more items. You create and use iterators in a way analogous to the use of an integer variable to step through an array.