Algorithm Conventions
Algorithm Conventions
The descriptions of the algorithm template functions employ several shorthand phrases:
- The phrase "in the range
[A, B)
" means the sequence of zero or more discrete values beginning withA
up to but not includingB
. A range is valid only ifB
is reachable fromA
: You can storeA
in an objectN
(N = A
), increment the object zero or more times (++N
), and have the object compare equal toB
after a finite number of increments (N == B
). - The phrase "each
N
in the range[A, B)
" means thatN
begins with the valueA
and is incremented zero or more times until it equals the valueB
. The caseN == B
is not in the range. - The phrase "the lowest value of
N
in the range[A, B)
such that X" means that the condition X is determined for eachN
in the range[A, B)
until the condition X is met. - The phrase "the highest value of
N
in the range[A, B)
such that X" usually means that X is determined for eachN
in the range[A, B)
. The function stores inK
a copy ofN
each time the condition X is met. If any such store occurs, the function replaces the final value ofN
(which equalsB
) with the value ofK
. For a bidirectional or random-access iterator, however, it can also mean thatN
begins with the highest value in the range and is decremented over the range until the condition X is met. - Expressions such as
X - Y
, whereX
andY
can be iterators other than random-access iterators, are intended in the mathematical sense. The function does not necessarily evaluateoperator-
if it must determine such a value. The same is true for expressions such asX + N
andX - N
, whereN
is an integer type.
Several algorithms use a predicate that must impose a strict weak ordering on pairs of elements from a sequence. For the predicate pr(X, Y)
:
- "strict" means that
pr(X, X)
is false - "weak" means that
X
andY
have an equivalent ordering if!pr(X, Y) && !pr(Y, X)
(X == Y
need not be defined) - "ordering" means that
pr(X, Y) && pr(Y, Z)
impliespr(X, Z)
Some of these algorithms implicitly use the predicate X < Y
. Other predicates that typically satisfy the "strict weak ordering" requirement are X > Y
, less
(X, Y)
, and greater
(X, Y)
. Note, however, that predicates such as X <= Y
and X >= Y
do not satisfy this requirement.
A sequence of elements designated by iterators in the range [first, last)
is "a sequence ordered by operator<
" if, for each N
in the range [0, last - first)
and for each M
in the range (N, last - first)
the predicate !(*(first + M) < *(first + N))
is true. (Note that the elements are sorted in ascending order.) The predicate function operator<
, or any replacement for it, must not alter either of its operands. Moreover, it must impose a strict weak ordering on the operands it compares.
A sequence of elements designated by iterators in the range [first, last)
is "a heap ordered by operator<
" if, for each N
in the range [1, last - first)
the predicate !(*first < *(first + N))
is true. (The first element is the largest.) Its internal structure is otherwise known only to the template functions make_heap
, pop_heap
, and push_heap
. As with an ordered sequence, the predicate function operator<
, or any replacement for it, must not alter either of its operands, and it must impose a strict weak ordering on the operands it compares.