beautypg.com

HP Integrity NonStop J-Series User Manual

Page 136

background image

Count() : N(0) { } // 2
int operator++() { return ++N; } // 3
operator int() { return N; } // 4
};

unsigned hashString ( const RWCString& str ) // 5
{ return str.hash(); }

main() {

RWTValHashDictionary Count /* Note: no comma here! */
RWDefHArgs(RWCString)> hmap(hashString); //6

RWCString token;
while ( cin >> token ) // 7
++hmap[token]; // 8

RWTValHashDictionaryIterator next(hmap); // 9

cout.setf(ios::left, ios::adjustfield); // 10
while ( ++next ) // 11
cout << setw(20) << next.key()
<< " " << setw(10) << next.value() << endl; // 12

return 0;
}

Program Input:
How much wood could a woodchuck chuck if a woodchuck could chuck wood ?

Program Output:
much 1
wood 2
a 2
if 1
woodchuck 2
could 2
chuck 2
How 1
? 1

In the code above, the problem is to read an input file, break it up into tokens separated by white space,
count the number of occurrences of each token, and then print the results. The general approach is to use a
dictionary to map each token to its respective count. Here's a line-by-line description:

//1

This is a class used as the value part of the dictionary.

//2

A default constructor is supplied that zeros out the count.

This manual is related to the following products: