Tools.h++ Manual

12-8 104011 Tandem Computers Incorporated
12
Suppose you were maintaining a mailing list. Given a person's name, you
want to find his or her address. In this case, you would want to search for a
name that is equal to the name at hand. A
Dictionary
would be appropriate.
The key to the
Dictionary
would be the name, the value would be the
address.
Suppose you are writing a hypertext application. You need to know in which
document a particular graphic occurs. This might be done by keeping a
Dictionary
of graphics and their corresponding document. In this case
however, you would want an
IdentityDictionary
because you need to
know in which document a particular graphic occurs. The graphic acts as the
key, the document as the value.
Suppose you were maintaining a disk cache. You might want to know whether
a particular object is resident in memory. In this case, an
IdentitySet
might
be appropriate. Given an object, you can check to see whether it exists in
memory.
12.3 Iterators
Many of the collection classes have an associated iterator.
The advantage of an iterator is that it maintains an internal state, allowing two
important benefits: more than one iterator can be constructed from the same
collection, and all of the items need not be visited in a single sweep.
Iterators are always constructed from the collection itself. For example:
RWBinaryTree bt;
.
.
.
RWBinaryTreeIterator bti (bt);
Caution – Immediately after construction (or after calling
reset()
), the state
of the iterator is undefined. You must either advance it or position it before
using its current state (i.e., position).
!