Iterators for Collection Classes
For the RWCollectable-based collection classes, the associated iterator classes are the main means of iterating through the collection. The remainder of this section discusses the use of these iterator classes. (The templatized collection classes use the C++ Standard Library-compliant iterator methods as described in the section C++ Standard Library Iterators.)
The advantage of the iterator class is that it maintains its own internal state, thus allowing two important benefits:
*More than one iterator can be constructed from the same collection class.
*All of the items need not be visited in a single sweep.
Iterators are always constructed from the collection class itself, as in this example:
 
RWBinaryTree bt;
.
.
.
RWBinaryTreeIterator bti(bt);
Immediately after construction, or after reset() is called, the state of the iterator is undefined. You must either advance it or position it before using its current state or position.
For traditional Essential Tools Module iterators—those declared as a distinct class related to the collection class—the rule is “advance and then return,” which is actually patterned after Stroustrup (1986, Section 7.3.2). However, iterators obtained directly from classes implemented using the C++ Standard Library differ. In keeping with the standard for container classes, they follow the precept: If you obtain an iterator using the begin() or end() method, or using an algorithm which returns an iterator, you have a “Standard Library” iterator. The ANSI standard describes container iterators in great detail. A Standard Library iterator must always be compared against that collection's end() iterator to discover if it truly references an item in the container, or if it is “one-past-the-last-element.”