Sorting and sorted list operations, Searching operations, In place transformations – HP Integrity NonStop H-Series User Manual
Page 73
) can be used for this purpose. The following statements, for example, test to see whether
an integer list contains the element 17.
int num = 0;
count(list_five.begin(), list_five.end(), 17, num);
if (num > 0)
cout << "contains a 17" << endl;
else
cout << "does not contain a 17" << endl;
if (find(list_five.begin(), list_five.end(), 17) != list_five.end())
cout << "contains a 17" << endl;
else
cout << "does not contain a 17" << endl;
Sorting and Sorted List Operations
The member function sort() places elements into ascending order. If a comparison operator other than
< is desired, it can be supplied as an argument.
list_ten.sort ( ); // place elements into sequence
list_twelve.sort (widgetCompare); // sort with widget compare
// function
Once a list has been sorted, a number of the generic algorithms for ordered collections can be used
with lists. These are described in detail in
.
Searching Operations
The various forms of searching functions described in
, namely find(), find_if(), adjacent
find(), mismatch(), max_element(), min_element() or search() can be applied to list. In all cases the
result is an iterator, which can be dereferenced to discover the denoted element, or used as an
argument in a subsequent operation.
Verify Search Results
In Place Transformations
A number of operations can be applied to lists in order to transform them in place. Some of these are
provided as member functions. Others make use of some of the generic functions described in
.
For a list, the member function reverse() reverses the order of elements in the list.
list_ten.reverse(); // elements are now reversed
The generic algorithm transform() (
Transform One or Two Sequences
) can be used to modify
every value in a container, by simply using the same container as both input and as result for the
operation. The following, for example, increments each element of a list by one. To construct the
necessary unary function, the first argument of the binary integer addition function is bound to the