Tools.h++ Manual
104011 Tandem Computers Incorporated 15-9
15
These functions test how many objects the collection contains and whether it
contains a particular object. The function
isEmpty()
returns true if the
collection contains no objects. The function
entries()
returns the total
number of objects that the collection contains.
The function
contains()
returns
TRUE
if the argument is equal to an item
within the collection. The meaning of "is equal to" depends on the collection
and the type of object being tested. Hashing collections use the virtual
function
isEqual()
to test for equality (with the
hash()
function used to
narrow the choices). Sorted collections search for an item that "compares
equal" (i.e.
compareTo()
returns zero) to the argument.
The virtual function
occurrencesOf()
is similar to
contains()
, but returns
the number of items that are equal to the argument.
The virtual function
find()
returns a pointer to an item that is equal to its
argument.
Here is an example that builds on the example in “Example” on page 4 and
uses some of these functions:
Code Example 15-1
#define RW_STD_TYPEDEFS 1
#include <rw/bintree.h> // 1
#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
sc.insert(new RWCollectableString("Mary")); // 8
cout << sc.entries() << "\n"; // 9
RWCollectableString dummy("Mary"); // 10
RWCollectable* t = sc.find( &dummy ); // 11