Tools.h++ Manual

15-4 104011 Tandem Computers Incorporated
15
15.2 Example
To get us oriented, it is always good to look at an example. This code uses a
SortedCollection
to store and order a set of
RWCollectableStrings
. We
will go through it line-by-line and explain what is happening:
Program output:
Bill
George
Mary
Throkmorton
SortedCollection
is actually a typedef for a
RWBinaryTree
. Objects
inserted into it are stored in order according to relative values returned by the
virtual function
compareTo()
(see “Virtual function compareTo()” on page 7
in Chapter 17, “Designing an RWCollectable Class”).
#define RW_STD_TYPEDEFS 1 // 1
#include <rw/bintree.h>
#include <rw/collstr.h> // 2
#include <rw/rstream.h>
main()
{
// Construct an empty SortedCollection
SortedCollection sc; // 3
// Insert some RWCollectableStrings:
sc.insert(new RWCollectableString("George")); // 4
sc.insert(new RWCollectableString("Mary")); // 5
sc.insert(new RWCollectableString("Bill")); // 6
sc.insert(new RWCollectableString("Throkmorton")); // 7
// Now iterate through the collection,
// printing all members:
RWCollectableString* str; // 8
SortedCollectionIterator sci(sc); // 9
while( str = (RWCollectableString*)sci() ) // 10
cout << *str << endl; // 11
return 0;
}