Tools.h++ Manual

22-32 104011 Tandem Computers Incorporated
22
must be searched linearly. The number of buckets can be changed by calling
member function
resize()
. This is relatively expensive because all of the
keys must be rehashed.
If you wish for this to be done automatically, then you can subclass from this
class and implement your own special
insert()
and
remove()
functions
which perform a
resize()
as necessary.
Example
Code Example 22-2
#include <rw/tphdict.h>
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <rw/rstream.h>
unsigned hashString(const RWCString& str){return str.hash();}
main()
{
RWTPtrHashDictionary<RWCString, RWDate> birthdays(hashString);
birthdays.insertKeyAndValue
(
new RWCString("John"),
new RWDate(12, "April", 1975)
);
birthdays.insertKeyAndValue
(
new RWCString("Ivan"),
new RWDate(2, "Nov", 1980)
);
// Alternative syntax:
birthdays[new RWCString("Susan")] =
new RWDate(30, "June", 1955);
birthdays[new RWCString("Gene")] =
new RWDate(5, "Jan", 1981);
// Print a birthday:
RWCString key("John");
cout << *birthdays[&key] << endl;
return 0;
}