#include "set.h"
Public Member Functions | |
| Iterator () | |
| bool | hasNext () |
| ElemType | next () |
Friends | |
| class | Set |
The idiomatic code for accessing elements using an iterator is to create the iterator from the collection and then enter a loop that calls next() while hasNext() is true, like this:
Set<int>::Iterator itr = set.iterator(); while (itr.hasNext()) int num = itr.next();
| Set< ElemType >::Iterator::Iterator | ( | ) |
The Iterator for Set tracks a pointer to the original Set and a vector containing of the elems that was assembled at the time of iteration creation. The iterator tracks an index into that vector that identifies the next key to return. Each use of next() increments that index, hasNext() verifies the index < the vector's size. This is an example of an "offline" iterator because it copies the set's data, instead of accessing it in-place. This is a bit inefficient (since it copies) but it makes the iterator simplier to write and sidesteps any problems where the client is modifying the set during iteration.
| bool Set< ElemType >::Iterator::hasNext | ( | ) |
| ElemType Set< ElemType >::Iterator::next | ( | ) |
friend class Set [friend] |
1.5.1