beautypg.com

HP Integrity NonStop J-Series User Manual

Page 137

background image

//3

We supply a prefix increment operator. This will be used to increment the count in a convenient
and pleasant way.

//4

A conversion operator is supplied that allows Count to be converted to an int. This will be used to
print the results. Alternatively, we could have supplied an overloaded operator<<() to teach a Count
how to print itself, but this is easier.

//5

This is a function that must be supplied to the dictionary constructor. Its job is to return a hash
value given an argument of the type of the key. Note that Tools.h++ supplies static hash member
functions for classes

RWCString

,

RWDate

,

RWTime

, and

RWWString

that can be used in place of a

user-supplied function. To keep the example general, we chose a user-defined function rather than
one of the static hash member functions defined by Tools.h++.

//6

Here the dictionary is constructed. Given a key, the dictionary can be used to look up a value. In
this case, the key will be of type

RWCString

, the value of type Count. The constructor requires a

single argument: a pointer to a function that will return a hash value, given a key. This function was
defined on line 5 above. Note that we used the RWDefHArgs(T) macro to ensure that the program
will be portable among platforms with and without the Standard C++ Library.

//7

Tokens are read from the input stream into an

RWCString

. This will continue until an EOF is

encountered. How does this work? The expression cin >> token reads a single token and returns an
ostream&. Class ostream has a type conversion operator to void*, which is what the while loop
will actually be testing. Operator void* returns this if the stream state is "good", and zero
otherwise. 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, and the ios
entry in the class reference guide that comes with your compiler.

//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 class. For all template iterators, the

prefix increment operator advances the iterator, then tests whether it has gone past the end of the
collection class.

//12 The key and value at the position of the iterator are printed.

This manual is related to the following products: