beautypg.com

Searching operations, Find an element satisfying a condition, Chapter 13 – HP Integrity NonStop H-Series User Manual

Page 150: Find an element, Satisfying a condition, Find an, Element satisfying a condition, Chapter, Searching opertations, Find an element satisfying a

background image

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

©Copyright 1996 Rogue Wave Software

Searching Operations

The next category of algorithms we will describe are those that are used to locate elements
within a sequence that satisfy certain properties. Most commonly the result of a search is then
used as an argument to a further operation, such as a copy (

Chapter 13:

Partition a Sequence into

Two Groups

), a partition (

Chapter 13:

Copy One Sequence into Another Sequence

) or an in-place

merge (

Chapter 13:

Merge two Adjacent Sequences into One

)

Obtaining the Source

The searching routines described in this section return an iterator that identifies the first element
that satisfies the search condition. It is common to store this value in an iterator variable, as
follows:

list::iterator where;
where = find(aList.begin(), aList.end(), 7);

If you want to locate all the elements that satisfy the search conditions you must write a loop. In
that loop, the value yielded by a previous search is first advanced (since otherwise the value
yielded by the previous search would once again be returned), and the resulting value is used as
a starting point for the new search. For example, the following loop from the adjacent_find()
example program (

Chapter 13:

Find Consecutive Duplicate Elements

) will print the value of all

repeated characters in a string argument.

Check Search Results

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

Many of the searching algorithms have an optional argument that can specify a function to be
used to compare elements, in place of the equality operator for the container element type
(operator ==). In the descriptions of the algorithms we write these optional arguments inside a
square bracket, to indicate they need not be specified if the standard equality operator is
acceptable.

This manual is related to the following products: