beautypg.com

HP Integrity NonStop H-Series User Manual

Page 155

background image

starting position, without an ending position. It is assumed (but not checked) that the second
sequence contains at least as many elements as the first. The arguments and return type for
mismatch() can be described as follows:

pair mismatch
(InputIterator first1, InputIterator last1,
InputIterator first2 [, BinaryPredicate ] );

The elements of the two sequences are examined in parallel, element by element. When a
mismatch is found, that is, a point where the two sequences differ, then a pair containing
iterators denoting the locations of the two differing elements is constructed and returned. If the
first sequence becomes exhausted before discovering any mismatched elements, then the
resulting pair contains the ending value for the first sequence, and the last value examined in the
second sequence. (The second sequence need not yet be exhausted).

The example program illustrates the use of this procedure. The function mismatch_test() takes
as arguments two string values. These are lexicographically compared and a message printed
indicating their relative ordering. (This is similar to the analysis performed by the
lexicographic_compare() algorithm, although that function simply returns a boolean value.)
Because the mismatch() algorithm assumes the second sequence is at least as long as the first, a
comparison of the two string lengths is performed first, and the arguments are reversed if the
second string is shorter than the first. After the call on mismatch() the elements of the resulting
pair are separated into their component parts. These parts are then tested to determine the
appropriate ordering.

void mismatch_test (char * a, char * b)
// illustrate the use of the mismatch algorithm
{
pair differPositions(0, 0);
char * aDiffPosition;
char * bDiffPosition;

if (strlen(a) < strlen(b)) {
// make sure longer string is second
differPositions = mismatch(a, a + strlen(a), b);
aDiffPosition = differPositions.first;
bDiffPosition = differPositions.second;
}
else {
differPositions = mismatch(b, b + strlen(b), a);
// note following reverse ordering
aDiffPosition = differPositions.second;
bDiffPosition = differPositions.first;
}

// compare resulting values
cout << "string " << a;
if (*aDiffPosition == *bDiffPosition)

This manual is related to the following products: