Comparator classes

Figure 1 shows the six major concrete classes of comparators.


As you might guess from looking at this diagram, the type of comparator you create depends on the type of collection you intend to use it with. For the time being ignore the comparators that use overloaded C++ operators and focus on the three major classes of comparators shown in Figure 1.


The decision tree diagram at the beginning of the chapter is a useful reference at this point. By looking at the types of collections in this diagram in relationship to the comparators presented in the figure above, you can almost guess what type of comparator a given type of collection will require.

Sets are not ordered, so a simple TMCollectibleComparator will do for sets. Sorted collections however need to be able to compare for equality (==, !=) as well as for relational inequality (<, >, <=, >=). The Compare member function of a TMCollectibleComparator or a TMOrderableCollectibleComparator determines equality. The OrderedCompare member function is special to TMOrderableCollectibleComparator and is used to order elements inside sorted collections (and possibly in priority queues).

Dictionaries, like sets are not ordered so you might be tempted to think they can use TMCollectibleComparators too. But Dictionaries are more complicated. The elements of dictionaries are key-value pairs so the proper comparator to use is TMCollectibleKeyValuePairComparator. In addition to the standard Compare member function, dictionary comparators let you compare elements based on either the element key (CompareKey) or the element value (CompareValue).

The Compare member function in descendants of TComparator only tells half the story. GetHash plays an equally important role in determining if two elements of a collection are equal. The role of GetHash in determining equality is covered in the section "Hashing, equality, and comparison" on page 60.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker