Tools.h++ Manual
104011 Tandem Computers Incorporated 13-9
13
which is what the while loop will actually be testing.
Operator void*
returns "
this
" if the stream state is "good", otherwise zero. Because an
EOF causes the stream state to turn to "not good", the while loop will be
broken when an EOF is encountered. See the
RWCString
entry in the
class reference guide and the
ios
entry in the class reference guide that
comes with your compiler for more details.
8. Here's where all the magic occurs. Object
map
is the dictionary. It has an
overloaded
operator[]
that takes an argument of the type of the key
and returns a reference to its associated value. Recall that the type of the
value is a
Count
. Hence,
map[token]
will be of type
Count
. As we
saw on line 3,
Count
has an overloaded prefix increment operator. This
is invoked on the
Count
, thereby increasing its value.
What if the key isn't in the dictionary? Then the overloaded
operator[]
will insert it, along with a brand new value built using the
default constructor of the value's class. This was defined on line 2 to
initialize the count to zero.
9. Now it comes time to print the results. We start by defining an iterator
that will sweep over the dictionary, returning each key and value.
10. The "field width" of the output stream is adjusted to make things pretty.
11. The iterator is advanced until it reaches the end of the collection. For all
template iterators, the prefix increment operator advances the iterator
then tests whether it has gone past the end of the collection.
12. The key and value at the position of the iterator are printed.