beautypg.com

Find a subsequence within a sequence, Find consecutive duplicate elements – HP Integrity NonStop H-Series User Manual

Page 152

background image

Find Consecutive Duplicate Elements

The adjacent_find() algorithm is used to discover the first element in a sequence equal to the
next immediately following element. For example, if a sequence contained the values 1 4 2 5 6
6 7 5, the algorithm would return an iterator corresponding to the first 6 value. If no value
satisfying the condition is found, then the end-of-sequence iterator is returned. The declaration
of the algorithm is as follows:

ForwardIterator adjacent_find (ForwardIterator first,
ForwardIterator last [, BinaryPredicate ] );

The first two arguments specify the sequence to be examined. The optional third argument must
be a binary predicate (a binary function returning a boolean value). If present, the binary
function is used to test adjacent elements, otherwise the equality operator (operator ==) is used.

The example program searches a text string for adjacent letters. In the example text these are
found in positions 5, 7, 9, 21 and 37. The increment is necessary inside the loop in order to
avoid the same position being discovered repeatedly.

void adjacent_find_example ()
// illustrate the use of the adjacent_find instruction
{
char * text = "The bookkeeper carefully opened the door.";

char * start = text;
char * stop = text + strlen(text);
char * where = start;

cout << "In the text: " << text << endl;
while ((where = adjacent_find(where, stop)) != stop) {
cout << "double " << *where
<< " in position " << where - start << endl;
++where;
}
}

Find a Subsequence within a Sequence

The algorithm search() is used to locate the beginning of a particular subsequence within a
larger sequence. The easiest example to understand is the problem of looking for a particular
substring within a larger string, although the algorithm can be generalized to other uses. The
arguments are assumed to have at least the capabilities of forward iterators.

ForwardIterator search
(ForwardIterator first1, ForwardIterator last1,
ForwardIterator first2, ForwardIterator last2
[, BinaryPredicate ]);

This manual is related to the following products: