Find() and friends – HP Integrity NonStop J-Series User Manual
Page 159

find() and Friends
You can use the following virtual functions to test how many objects a collection contains, and
whether it contains a particular object:
virtual RWBoolean contains(const RWCollectable*) const;
virtual unsigned entries() const;
virtual RWCollectable* find(const RWCollectable*) const;
virtual RWBoolean isEmpty() const;
virtual unsigned occurrencesOf(const RWCollectable*) const;
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, after first hashing the argument to
reduce the number of possible candidates to those in one hash bucket. (Here it is important that all
items which are isEqual with each other hash to the same value!). Sorted collections search for an
item that compares equal to the argument; in other words, an item for which compareTo() returns
zero.
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.
The following example, which builds on the example in
, uses find() to find
occurrences of Mary in the collection, and occurrencesOf to find the number of times Mary
occurs:
#define RW_STD_TYPEDEFS 1
#include
#include
#include
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