beautypg.com

Comparators, More on total ordering – HP Integrity NonStop J-Series User Manual

Page 121

background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Comparators

The associative container-based and the sorted sequence-based collection classes maintain order
internally. This ordering is based on a comparison object, an instance of a comparator class you
must supply when instantiating the template. A comparator must contain a const member
operator(), the function-call operator, which takes two potential elements of the collection class
as arguments and returns a Boolean value. The returned value should be true if the first argument
must precede the second within the collection class, and false otherwise. Often, it is easiest to use
one of the function-object classes provided by the Standard C++ Library in the header file
. In particular, use less to maintain elements in increasing order, or greater
to maintain them in decreasing order. For example:

#include
#include
#include

RWTValSet > mySet1;
RWTValSet > mySet2;

Here mySet1 is a set of integers kept in increasing order, while mySet2 is a set of dates held in
decreasing order; that is, from the most recent to the oldest. You can use these comparators from
the Standard C++ Library as long as the expression (x < y) for the case of less, or (x > y) for
the case of greater, are valid expressions that induce a total ordering on objects of type T.

More on Total Ordering

As noted above, the comparator must induce a total ordering on the type of the items in the
collection class. This means that the function-call operator of the comparator must satisfy the
following two conditions

[15]

, assuming that comp is the comparison object and x, y, and z are

potential elements of the collection class, not necessarily distinct:

I. Exactly one of the following statements is true:
a) comp(x,y) is true and comp(y,x) is false
b) comp(x,y) is false and comp(y,x) is true
c) comp(x,y) is false and comp(y,x) is false
(or, in other words: not both comp(x,y) and comp(y,x)
are true)
II. If comp(x,y) and comp(y,z) are true, then so is

This manual is related to the following products: