beautypg.com

Remove() functions – HP Integrity NonStop J-Series User Manual

Page 160

background image

RWCollectableString dummy("Mary"); //10
RWCollectable* t = sc.find( &dummy ); //11

if(t){ //12
if(t->isA() == dummy.isA()) //13
cout << *(RWCollectableString*)t << "\n"; //14
}
else
cout << "Object not found.\n"; //15

cout << sc.occurrencesOf(&dummy) << "\n"; //16

sc.clearAndDestroy();
return 0;
}

Program Output:

5
Mary
2

Here's the line-by-line description:

//1 - //7 These lines are from the example in Chapter 13.

//8

Insert another instance with the value Mary.

//9

This statement prints out 5, the total number of entries in the sorted collection.

//10

A throwaway variable dummy is constructed, to be used to test for the occurrences of
strings containing Mary.

//11

The collection is asked to return a pointer to the first object encountered that compares
equal to the argument. A nil pointer (zero) is returned if there is no such object.

//12

The pointer is tested to make sure it is not nil.

//13

Paranoid check. In this example, it is obvious that the items in the collection must be of
type

RWCollectableString

. In general, it may not be obvious.

//14

Because of the results of step 13, the cast to an

RWCollectableString

pointer is safe. The

pointer is then dereferenced and printed.

//15

If the pointer t was nil, then an error message would have been printed here.

//16

The call to occurrencesOf() returns the number of items that compare equal to its
argument. In this case, two items are found, the two occurrences of Mary.

This manual is related to the following products: