completed the upper_bound description

This commit is contained in:
Frank B. Brokken 2019-05-17 14:06:17 +02:00
parent 7d913ebaa6
commit c4c2454513
5 changed files with 180 additions and 21 deletions

View file

@ -19,7 +19,7 @@ tt(false) otherwise.
search in the series of elements implied by the iterator range rangett(first,
last). The elements in the range must have been sorted by the tt(Comparator)
function object. tt(True) is returned if the element was found, tt(false)
otherwise. As illustrated by the following example, the function object's
otherwise. As illustrated by the following example, the function object
function's first parameter refers to an element in the iterator range, while
the function object's second parameter refers to tt(value).
)
@ -30,5 +30,6 @@ the function object's second parameter refers to tt(value).
If tt(value) is in fact present in the range of values, then this generic
algorithm doesn't answer the question where tt(value) is located. If that
question must be answered the generic algorithms link(lower_bound)(LOWERBOUND)
and link(upper_bound)(UPPERBOUND) can be used. Refer to section ref(UPPERBOUND)
for an extensive coverage of these algorithms.
and link(upper_bound)(UPPERBOUND) can be used. Refer to section
ref(UPPERBOUND) for an extensive example illustrating the use of these latter
two algorithms.

View file

@ -188,3 +188,4 @@ return 0;
}

View file

@ -0,0 +1,93 @@
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
typedef pair<int, char> pic;
pic picArr[] =
{ {1, 'f'}, {5, 'r'}, {5, 'a'}, {7, 'n'}, {8, 'k'} };
pic *picArrEnd = picArr + size(picArr);
cout << "Sequence: ";
for (auto &pair: picArr)
cout << '{' << pair.first << ',' << pair.second << "}, ";
cout << '\n';
auto iter = lower_bound(picArr, picArrEnd, 5,
[&](pic const &range, int value)
{
return range.first < value;
}
);
cout << " lower bound, <, {5,?} can be inserted before {" <<
iter->first << ',' << iter->second << "}\n";
iter = upper_bound(picArr, picArrEnd, 5,
[&](int value, pic const &range)
{
return value < range.first;
}
);
cout << " upper_bound, <, {5,?} can be inserted before {" <<
iter->first << ',' << iter->second << "}\n";
iter = upper_bound(picArr, picArrEnd, 9,
[&](int value, pic const &range)
{
return value < range.first;
}
);
cout << " upper_bound, <, {9,?} can be inserted " <<
( &*iter == picArrEnd ? "at the end" : "???") << '\n';
sort(picArr, picArrEnd,
[](pic const &lhs, pic const &rhs)
{
return lhs.first > rhs.first;
}
);
cout << "\nSequence: ";
for (auto &pair: picArr)
cout << '{' << pair.first << ',' << pair.second << "}, ";
cout << '\n';
iter = lower_bound(picArr, picArrEnd, 5,
[&](pic const &range, int value)
{
return range.first > value;
}
);
cout << " lower_bound, >, {5,?} can be inserted before {" <<
iter->first << ',' << iter->second << "}\n";
iter = upper_bound(picArr, picArrEnd, 5,
[&](int value, pic const &range)
{
return value > range.first;
}
);
cout << " upper_bound, >, {5,?} can be inserted before {" <<
iter->first << ',' << iter->second << "}\n";
iter = upper_bound(picArr, picArrEnd, 0,
[&](int value, pic const &range)
{
return value > range.first;
}
);
cout << " upper_bound, >, {0,?} can be inserted " <<
( &*iter == picArrEnd ? "at the end" : "???") << '\n';
}
// Displays:
// Sequence: {1,f}, {5,r}, {5,a}, {7,n}, {8,k},
// lower bound, <, {5,?} can be inserted before {5,r}
// upper_bound, <, {5,?} can be inserted before {7,n}
// upper_bound, <, {9,?} can be inserted at the end
//
// Sequence: {8,k}, {7,n}, {5,r}, {5,a}, {1,f},
// lower_bound, >, {5,?} can be inserted before {5,r}
// upper_bound, >, {5,?} can be inserted before {1,f}
// upper_bound, >, {0,?} can be inserted at the end

View file

@ -23,7 +23,9 @@ range rangett(first, last) must have been sorted using the tt(comp) function
tt(comp) function. An iterator to the first element for which the binary
predicate tt(comp), applied to the elements of the range and tt(value),
returns tt(false) is returned. If no such element is found, tt(last) is
returned. As illustrated by the following example, the function object's
returned.
As illustrated by the following example, the function object
function's first parameter refers to an element in the iterator range, while
the function object's second parameter refers to tt(value).
)
@ -34,5 +36,10 @@ the function object's second parameter refers to tt(value).
The link(binary_search)(BINSRCH) generic algorithm can be used to simply
determine whether or nog tt(value) is present in the iterator range. The
link(upper_bound)(UPPERBOUND) can be used to find the last element of a series
of values equal to tt(value). At the tt(upper_bound) section (ref(UPPERBOUND)
more extensive examples of tt(upper_bound) as well as tt(lower_bound).
of values equal to tt(value). The tt(upper_bound) section (ref(UPPERBOUND)
also contains an extensive example illustrating the use of tt(lower_bound) and
as tt(upper_bound).

View file

@ -10,21 +10,78 @@ ForwardIterator last, Type const &value, Compare comp);)
)
it() Description:
itemization(
it() The first prototype: the sorted elements (using ascending
sort) stored in the iterator range rangett(first, last) are searched for the
first element that is greater than tt(value). The returned iterator marks the
first location in the sequence where tt(value) can be inserted without
breaking the sorted order of the elements using ti(operator<) of the data type
to which the iterators point. If no such element is found, tt(last) is
returned.
it() The second prototype: the elements implied by the iterator
range rangett(first, last) must have been sorted using the tt(comp) function
or function object. Each element in the range is compared to tt(value) using
the tt(comp) function. An iterator to the first element for which the binary
predicate tt(comp), applied to the elements of the range and tt(value),
returns tt(true) is returned. If no such element is found, tt(last) is
returned.
it() The first prototype: the sorted elements (using ascending sort)
stored in the iterator range rangett(first, last) are searched for the first
element that is greater than tt(value). The returned iterator marks the first
location in the sequence where tt(value) can be inserted without breaking the
sorted order of the elements using ti(operator<) of the data type to which the
iterators point. If no such element is found, tt(last) is returned.
it() The second prototype: the elements implied by the iterator range
rangett(first, last) must have been sorted using the tt(comp) function or
function object. Each element in the range is compared to tt(value) using the
tt(comp) function. An iterator is returned pointing to the first element for
which the binary predicate tt(comp), applied to the elements of the range and
tt(value), returns tt(true). The tt(comp) function object function's first
parameter refers to tt(value) and the function object's second parameter
refers to an element in the iterator range.
Caveat: note that the tt(comp) object's parameters when using
tt(upper_bound) are swapped compared to the parameters expected by
tt(lower_bound).
it() When the values in the iterator range were sorted in ascending
order (i.e., using tt(operator<)) then tt(upper_bound) returns an iterator
pointing beyond the last of a series of values equal to tt(value), while
tt(lower_bound) returns an iterator pointing to the first of such a series of
equal values.
When the iterator range contains a series of values which are, according to
tt(comp), equal to tt(value) then tt(upper_bound) returns an iterator to the
first element beyond that series, while tt(lower_bound) returns an iterator to
the first element of that series.
The following program illustrates the various possibilities. Th program
illustrates both tt(lower_bound) and tt(upper_bound) and also illustrates the
situation where tt(value' Type) is unequal to the types of the values in the
iterator range. Specific comment is provided below the program's code.
)
it() Example:
verbinclude(-a examples/upperbound.cc)
verbinclude(-an examples/upperbound2.cc)
itemization(
it() Lines 7 thru 12: the iterator range consists of a series of
tt(pairs), sorted by their tt(first) members.
it() Lines 18 thru 23: tt(lower_bound) is called using a lambda
expression to define the tt(Compare) function object. Note (line
19) that a reference to a value in the iterator range is the
lambda expression's first parameter, while the target value is its
second parameter.
it() Lines 27 thru 32: here tt(upper_bound) is called, also using a
lambda expression. With tt(upper_bound) the target value is the
lambda expression's first parameter, while a reference to a value
in the iterator range is its second parameter.
it() Lines 57 thru 62, 66 thru 71, and 75 thru 80: after sorting the
values in the tt(picArr) array in descending order tt(lower_bound)
and tt(upper_bound) are again used. This time instead of using the
tt(<) operator the tt(>) operator should be used.
)
)
The link(binary_search)(BINSRCH) generic algorithm can be used to simply
determine whether or nog tt(value) is present in the iterator range. The
link(lower_bound)(LOWERBOUND) generic algorithm can be used to find the first
element of a series of values equal to tt(value).