tmp/tmpypxhy_tj/{from.md → to.md}
RENAMED
|
@@ -1,18 +1,20 @@
|
|
| 1 |
## Sorting and related operations <a id="alg.sorting">[[alg.sorting]]</a>
|
| 2 |
|
|
|
|
|
|
|
| 3 |
The operations in [[alg.sorting]] defined directly in namespace `std`
|
| 4 |
have two versions: one that takes a function object of type `Compare`
|
| 5 |
and one that uses an `operator<`.
|
| 6 |
|
| 7 |
`Compare` is a function object type [[function.objects]] that meets the
|
| 8 |
requirements for a template parameter named `BinaryPredicate`
|
| 9 |
[[algorithms.requirements]]. The return value of the function call
|
| 10 |
-
operation applied to an object of type `Compare`, when
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
For all algorithms that take `Compare`, there is a version that uses
|
| 16 |
`operator<` instead. That is, `comp(*i, *j) != false` defaults to
|
| 17 |
`*i < *j != false`. For algorithms other than those described in
|
| 18 |
[[alg.binary.search]], `comp` shall induce a strict weak ordering on the
|
|
@@ -48,10 +50,14 @@ pointing to the sequence and every non-negative integer `n` such that
|
|
| 48 |
bool(invoke(comp, invoke(proj, *(i + n)), invoke(proj, *i)))
|
| 49 |
```
|
| 50 |
|
| 51 |
is `false`.
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
A sequence \[`start`, `finish`) is *partitioned with respect to an
|
| 54 |
expression* `f(e)` if there exists an integer `n` such that for all
|
| 55 |
`0 <= i < (finish - start)`, `f(*(start + i))` is `true` if and only if
|
| 56 |
`i < n`.
|
| 57 |
|
|
@@ -283,13 +289,13 @@ with no parameters by those names.
|
|
| 283 |
`RandomAccessIterator` meets the *Cpp17ValueSwappable*
|
| 284 |
requirements [[swappable.requirements]], the type of `*result_first`
|
| 285 |
meets the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]]) and
|
| 286 |
*Cpp17MoveAssignable* ([[cpp17.moveassignable]]) requirements.
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
|
| 292 |
``` cpp
|
| 293 |
bool(invoke(comp, invoke(proj1, *a1), invoke(proj2, *y2))).
|
| 294 |
```
|
| 295 |
|
|
@@ -467,19 +473,21 @@ template<random_access_range R, class Comp = ranges::less, class Proj = identity
|
|
| 467 |
return ranges::nth_element(ranges::begin(r), nth, ranges::end(r), comp, proj);
|
| 468 |
```
|
| 469 |
|
| 470 |
### Binary search <a id="alg.binary.search">[[alg.binary.search]]</a>
|
| 471 |
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
number of steps
|
|
|
|
|
|
|
| 481 |
|
| 482 |
#### `lower_bound` <a id="lower.bound">[[lower.bound]]</a>
|
| 483 |
|
| 484 |
``` cpp
|
| 485 |
template<class ForwardIterator, class T>
|
|
@@ -759,19 +767,18 @@ range \[`i`, `last`), E(`*j`) is `false`. Returns:
|
|
| 759 |
- `i` for the overloads in namespace `std`.
|
| 760 |
- `{i, last}` for the overloads in namespace `ranges`.
|
| 761 |
|
| 762 |
*Complexity:* Let N = `last - first`:
|
| 763 |
|
| 764 |
-
- For the overloads with no `ExecutionPolicy`, at most N log N swaps,
|
| 765 |
but only 𝑂(N) swaps if there is enough extra memory. Exactly N
|
| 766 |
applications of the predicate and projection.
|
| 767 |
- For the overload with an `ExecutionPolicy`, 𝑂(N log N) swaps and 𝑂(N)
|
| 768 |
applications of the predicate.
|
| 769 |
|
| 770 |
``` cpp
|
| 771 |
-
template<class InputIterator, class OutputIterator1,
|
| 772 |
-
class OutputIterator2, class Predicate>
|
| 773 |
constexpr pair<OutputIterator1, OutputIterator2>
|
| 774 |
partition_copy(InputIterator first, InputIterator last,
|
| 775 |
OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred);
|
| 776 |
template<class ExecutionPolicy, class ForwardIterator, class ForwardIterator1,
|
| 777 |
class ForwardIterator2, class Predicate>
|
|
@@ -802,11 +809,11 @@ Let `proj` be `identity{}` for the overloads with no parameter named
|
|
| 802 |
`*first` is writable [[iterator.requirements.general]] to `out_true` and
|
| 803 |
`out_false`.
|
| 804 |
|
| 805 |
*Preconditions:* The input range and output ranges do not overlap.
|
| 806 |
|
| 807 |
-
[*Note 1*: For the overload with an `ExecutionPolicy`, there
|
| 808 |
performance cost if `first`’s value type does not meet the
|
| 809 |
*Cpp17CopyConstructible* requirements. — *end note*]
|
| 810 |
|
| 811 |
*Effects:* For each iterator `i` in \[`first`, `last`), copies `*i` to
|
| 812 |
the output range beginning with `out_true` if E(`*i`) is `true`, or to
|
|
@@ -991,16 +998,18 @@ template<bidirectional_range R, class Comp = ranges::less, class Proj = identity
|
|
| 991 |
return ranges::inplace_merge(ranges::begin(r), middle, ranges::end(r), comp, proj);
|
| 992 |
```
|
| 993 |
|
| 994 |
### Set operations on sorted structures <a id="alg.set.operations">[[alg.set.operations]]</a>
|
| 995 |
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
|
| 999 |
-
|
| 1000 |
-
|
| 1001 |
-
|
|
|
|
|
|
|
| 1002 |
|
| 1003 |
#### `includes` <a id="includes">[[includes]]</a>
|
| 1004 |
|
| 1005 |
``` cpp
|
| 1006 |
template<class InputIterator1, class InputIterator2>
|
|
@@ -1053,12 +1062,11 @@ keeping the remaining elements in the same order. — *end note*]
|
|
| 1053 |
comparisons and applications of each projection.
|
| 1054 |
|
| 1055 |
#### `set_union` <a id="set.union">[[set.union]]</a>
|
| 1056 |
|
| 1057 |
``` cpp
|
| 1058 |
-
template<class InputIterator1, class InputIterator2,
|
| 1059 |
-
class OutputIterator>
|
| 1060 |
constexpr OutputIterator
|
| 1061 |
set_union(InputIterator1 first1, InputIterator1 last1,
|
| 1062 |
InputIterator2 first2, InputIterator2 last2,
|
| 1063 |
OutputIterator result);
|
| 1064 |
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
|
|
@@ -1067,12 +1075,11 @@ template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
|
|
| 1067 |
set_union(ExecutionPolicy&& exec,
|
| 1068 |
ForwardIterator1 first1, ForwardIterator1 last1,
|
| 1069 |
ForwardIterator2 first2, ForwardIterator2 last2,
|
| 1070 |
ForwardIterator result);
|
| 1071 |
|
| 1072 |
-
template<class InputIterator1, class InputIterator2,
|
| 1073 |
-
class OutputIterator, class Compare>
|
| 1074 |
constexpr OutputIterator
|
| 1075 |
set_union(InputIterator1 first1, InputIterator1 last1,
|
| 1076 |
InputIterator2 first2, InputIterator2 last2,
|
| 1077 |
OutputIterator result, Compare comp);
|
| 1078 |
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
|
|
@@ -1266,11 +1273,11 @@ Returns
|
|
| 1266 |
comparisons and applications of each projection.
|
| 1267 |
|
| 1268 |
*Remarks:* If \[`first1`, `last1`) contains m elements that are
|
| 1269 |
equivalent to each other and \[`first2`, `last2`) contains n elements
|
| 1270 |
that are equivalent to them, the last max(m - n, 0) elements from
|
| 1271 |
-
\[`first1`, `last1`)
|
| 1272 |
|
| 1273 |
#### `set_symmetric_difference` <a id="set.symmetric.difference">[[set.symmetric.difference]]</a>
|
| 1274 |
|
| 1275 |
``` cpp
|
| 1276 |
template<class InputIterator1, class InputIterator2,
|
|
@@ -1349,10 +1356,12 @@ elements from \[`first1`, `last1`) if m > n, and the last n - m of these
|
|
| 1349 |
elements from \[`first2`, `last2`) if m < n. In either case, the
|
| 1350 |
elements are copied in order.
|
| 1351 |
|
| 1352 |
### Heap operations <a id="alg.heap.operations">[[alg.heap.operations]]</a>
|
| 1353 |
|
|
|
|
|
|
|
| 1354 |
A random access range \[`a`, `b`) is a *heap with respect to `comp` and
|
| 1355 |
`proj`* for a comparator and projection `comp` and `proj` if its
|
| 1356 |
elements are organized such that:
|
| 1357 |
|
| 1358 |
- With `N = b - a`, for all i, 0 < i < N,
|
|
@@ -1389,14 +1398,15 @@ template<random_access_range R, class Comp = ranges::less, class Proj = identity
|
|
| 1389 |
|
| 1390 |
Let `comp` be `less{}` and `proj` be `identity{}` for the overloads with
|
| 1391 |
no parameters by those names.
|
| 1392 |
|
| 1393 |
*Preconditions:* The range \[`first`, `last - 1`) is a valid heap with
|
| 1394 |
-
respect to `comp` and `proj`. For the overloads in namespace `std`,
|
| 1395 |
-
|
| 1396 |
-
|
| 1397 |
-
requirements ([[cpp17.
|
|
|
|
| 1398 |
|
| 1399 |
*Effects:* Places the value in the location `last - 1` into the
|
| 1400 |
resulting heap \[`first`, `last`).
|
| 1401 |
|
| 1402 |
*Returns:* `last` for the overloads in namespace `ranges`.
|
|
@@ -1466,14 +1476,15 @@ template<random_access_range R, class Comp = ranges::less, class Proj = identity
|
|
| 1466 |
```
|
| 1467 |
|
| 1468 |
Let `comp` be `less{}` and `proj` be `identity{}` for the overloads with
|
| 1469 |
no parameters by those names.
|
| 1470 |
|
| 1471 |
-
*Preconditions:* For the overloads in namespace `std`,
|
| 1472 |
-
`
|
| 1473 |
-
|
| 1474 |
-
([[cpp17.
|
|
|
|
| 1475 |
|
| 1476 |
*Effects:* Constructs a heap with respect to `comp` and `proj` out of
|
| 1477 |
the range \[`first`, `last`).
|
| 1478 |
|
| 1479 |
*Returns:* `last` for the overloads in namespace `ranges`.
|
|
@@ -1625,19 +1636,19 @@ template<class T, class Proj = identity,
|
|
| 1625 |
```
|
| 1626 |
|
| 1627 |
*Preconditions:* For the first form, `T` meets the
|
| 1628 |
*Cpp17LessThanComparable* requirements ([[cpp17.lessthancomparable]]).
|
| 1629 |
|
| 1630 |
-
*Returns:* The smaller value.
|
| 1631 |
-
|
| 1632 |
-
*Remarks:* Returns the first argument when the arguments are equivalent.
|
| 1633 |
-
An invocation may explicitly specify an argument for the template
|
| 1634 |
-
parameter `T` of the overloads in namespace `std`.
|
| 1635 |
|
| 1636 |
*Complexity:* Exactly one comparison and two applications of the
|
| 1637 |
projection, if any.
|
| 1638 |
|
|
|
|
|
|
|
|
|
|
| 1639 |
``` cpp
|
| 1640 |
template<class T>
|
| 1641 |
constexpr T min(initializer_list<T> r);
|
| 1642 |
template<class T, class Compare>
|
| 1643 |
constexpr T min(initializer_list<T> r, Compare comp);
|
|
@@ -1655,20 +1666,19 @@ template<input_range R, class Proj = identity,
|
|
| 1655 |
*Preconditions:* `ranges::distance(r) > 0`. For the overloads in
|
| 1656 |
namespace `std`, `T` meets the *Cpp17CopyConstructible* requirements.
|
| 1657 |
For the first form, `T` meets the *Cpp17LessThanComparable* requirements
|
| 1658 |
([[cpp17.lessthancomparable]]).
|
| 1659 |
|
| 1660 |
-
*Returns:* The smallest value in the input range.
|
| 1661 |
-
|
| 1662 |
-
*Remarks:* Returns a copy of the leftmost element when several elements
|
| 1663 |
-
are equivalent to the smallest. An invocation may explicitly specify an
|
| 1664 |
-
argument for the template parameter `T` of the overloads in namespace
|
| 1665 |
-
`std`.
|
| 1666 |
|
| 1667 |
*Complexity:* Exactly `ranges::distance(r) - 1` comparisons and twice as
|
| 1668 |
many applications of the projection, if any.
|
| 1669 |
|
|
|
|
|
|
|
|
|
|
| 1670 |
``` cpp
|
| 1671 |
template<class T>
|
| 1672 |
constexpr const T& max(const T& a, const T& b);
|
| 1673 |
template<class T, class Compare>
|
| 1674 |
constexpr const T& max(const T& a, const T& b, Compare comp);
|
|
@@ -1679,19 +1689,19 @@ template<class T, class Proj = identity,
|
|
| 1679 |
```
|
| 1680 |
|
| 1681 |
*Preconditions:* For the first form, `T` meets the
|
| 1682 |
*Cpp17LessThanComparable* requirements ([[cpp17.lessthancomparable]]).
|
| 1683 |
|
| 1684 |
-
*Returns:* The larger value.
|
| 1685 |
-
|
| 1686 |
-
*Remarks:* Returns the first argument when the arguments are equivalent.
|
| 1687 |
-
An invocation may explicitly specify an argument for the template
|
| 1688 |
-
parameter `T` of the overloads in namespace `std`.
|
| 1689 |
|
| 1690 |
*Complexity:* Exactly one comparison and two applications of the
|
| 1691 |
projection, if any.
|
| 1692 |
|
|
|
|
|
|
|
|
|
|
| 1693 |
``` cpp
|
| 1694 |
template<class T>
|
| 1695 |
constexpr T max(initializer_list<T> r);
|
| 1696 |
template<class T, class Compare>
|
| 1697 |
constexpr T max(initializer_list<T> r, Compare comp);
|
|
@@ -1709,20 +1719,19 @@ template<input_range R, class Proj = identity,
|
|
| 1709 |
*Preconditions:* `ranges::distance(r) > 0`. For the overloads in
|
| 1710 |
namespace `std`, `T` meets the *Cpp17CopyConstructible* requirements.
|
| 1711 |
For the first form, `T` meets the *Cpp17LessThanComparable* requirements
|
| 1712 |
([[cpp17.lessthancomparable]]).
|
| 1713 |
|
| 1714 |
-
*Returns:* The largest value in the input range.
|
| 1715 |
-
|
| 1716 |
-
*Remarks:* Returns a copy of the leftmost element when several elements
|
| 1717 |
-
are equivalent to the largest. An invocation may explicitly specify an
|
| 1718 |
-
argument for the template parameter `T` of the overloads in namespace
|
| 1719 |
-
`std`.
|
| 1720 |
|
| 1721 |
*Complexity:* Exactly `ranges::distance(r) - 1` comparisons and twice as
|
| 1722 |
many applications of the projection, if any.
|
| 1723 |
|
|
|
|
|
|
|
|
|
|
| 1724 |
``` cpp
|
| 1725 |
template<class T>
|
| 1726 |
constexpr pair<const T&, const T&> minmax(const T& a, const T& b);
|
| 1727 |
template<class T, class Compare>
|
| 1728 |
constexpr pair<const T&, const T&> minmax(const T& a, const T& b, Compare comp);
|
|
@@ -1736,16 +1745,16 @@ template<class T, class Proj = identity,
|
|
| 1736 |
*Preconditions:* For the first form, `T` meets the
|
| 1737 |
*Cpp17LessThanComparable* requirements ([[cpp17.lessthancomparable]]).
|
| 1738 |
|
| 1739 |
*Returns:* `{b, a}` if `b` is smaller than `a`, and `{a, b}` otherwise.
|
| 1740 |
|
| 1741 |
-
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1742 |
-
template parameter `T` of the overloads in namespace `std`.
|
| 1743 |
-
|
| 1744 |
*Complexity:* Exactly one comparison and two applications of the
|
| 1745 |
projection, if any.
|
| 1746 |
|
|
|
|
|
|
|
|
|
|
| 1747 |
``` cpp
|
| 1748 |
template<class T>
|
| 1749 |
constexpr pair<T, T> minmax(initializer_list<T> t);
|
| 1750 |
template<class T, class Compare>
|
| 1751 |
constexpr pair<T, T> minmax(initializer_list<T> t, Compare comp);
|
|
@@ -1768,17 +1777,17 @@ requirements ([[cpp17.lessthancomparable]]).
|
|
| 1768 |
|
| 1769 |
*Returns:* Let `X` be the return type. Returns `X{x, y}`, where `x` is a
|
| 1770 |
copy of the leftmost element with the smallest value and `y` a copy of
|
| 1771 |
the rightmost element with the largest value in the input range.
|
| 1772 |
|
| 1773 |
-
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1774 |
-
template parameter `T` of the overloads in namespace `std`.
|
| 1775 |
-
|
| 1776 |
*Complexity:* At most (3/2)`ranges::distance(r)` applications of the
|
| 1777 |
corresponding predicate and twice as many applications of the
|
| 1778 |
projection, if any.
|
| 1779 |
|
|
|
|
|
|
|
|
|
|
| 1780 |
``` cpp
|
| 1781 |
template<class ForwardIterator>
|
| 1782 |
constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
|
| 1783 |
|
| 1784 |
template<class ExecutionPolicy, class ForwardIterator>
|
|
@@ -1788,12 +1797,11 @@ template<class ExecutionPolicy, class ForwardIterator>
|
|
| 1788 |
template<class ForwardIterator, class Compare>
|
| 1789 |
constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
|
| 1790 |
Compare comp);
|
| 1791 |
template<class ExecutionPolicy, class ForwardIterator, class Compare>
|
| 1792 |
ForwardIterator min_element(ExecutionPolicy&& exec,
|
| 1793 |
-
ForwardIterator first, ForwardIterator last,
|
| 1794 |
-
Compare comp);
|
| 1795 |
|
| 1796 |
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
|
| 1797 |
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
|
| 1798 |
constexpr I ranges::min_element(I first, S last, Comp comp = {}, Proj proj = {});
|
| 1799 |
template<forward_range R, class Proj = identity,
|
|
@@ -1873,23 +1881,25 @@ template<class ExecutionPolicy, class ForwardIterator, class Compare>
|
|
| 1873 |
minmax_element(ExecutionPolicy&& exec,
|
| 1874 |
ForwardIterator first, ForwardIterator last, Compare comp);
|
| 1875 |
|
| 1876 |
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
|
| 1877 |
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
|
| 1878 |
-
constexpr ranges::
|
| 1879 |
ranges::minmax_element(I first, S last, Comp comp = {}, Proj proj = {});
|
| 1880 |
template<forward_range R, class Proj = identity,
|
| 1881 |
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
|
| 1882 |
-
constexpr ranges::
|
| 1883 |
ranges::minmax_element(R&& r, Comp comp = {}, Proj proj = {});
|
| 1884 |
```
|
| 1885 |
|
| 1886 |
*Returns:* `{first, first}` if \[`first`, `last`) is empty, otherwise
|
| 1887 |
`{m, M}`, where `m` is the first iterator in \[`first`, `last`) such
|
| 1888 |
that no iterator in the range refers to a smaller element, and where `M`
|
| 1889 |
-
is the last iterator[^5]
|
| 1890 |
-
|
|
|
|
|
|
|
| 1891 |
|
| 1892 |
*Complexity:* Let N be `last - first`. At most
|
| 1893 |
$\max(\bigl\lfloor{\frac{3}{2}} (N-1)\bigr\rfloor, 0)$ comparisons and
|
| 1894 |
twice as many applications of the projection, if any.
|
| 1895 |
|
|
@@ -1907,16 +1917,19 @@ template<class T, class Proj = identity,
|
|
| 1907 |
```
|
| 1908 |
|
| 1909 |
Let `comp` be `less{}` for the overloads with no parameter `comp`, and
|
| 1910 |
let `proj` be `identity{}` for the overloads with no parameter `proj`.
|
| 1911 |
|
| 1912 |
-
*Preconditions:*
|
| 1913 |
-
|
| 1914 |
-
|
|
|
|
| 1915 |
|
| 1916 |
-
*Returns:* `lo` if
|
| 1917 |
-
`bool(comp(proj
|
|
|
|
|
|
|
| 1918 |
|
| 1919 |
[*Note 1*: If NaN is avoided, `T` can be a floating-point
|
| 1920 |
type. — *end note*]
|
| 1921 |
|
| 1922 |
*Complexity:* At most two comparisons and three applications of the
|
|
@@ -1981,11 +1994,11 @@ the sequences yields the same result as the comparison of the first
|
|
| 1981 |
corresponding pair of elements that are not equivalent.
|
| 1982 |
|
| 1983 |
[*Example 1*:
|
| 1984 |
|
| 1985 |
`ranges::lexicographical_compare(I1, S1, I2, S2, Comp, Proj1, Proj2)`
|
| 1986 |
-
|
| 1987 |
|
| 1988 |
``` cpp
|
| 1989 |
for ( ; first1 != last1 && first2 != last2 ; ++first1, (void) ++first2) {
|
| 1990 |
if (invoke(comp, invoke(proj1, *first1), invoke(proj2, *first2))) return true;
|
| 1991 |
if (invoke(comp, invoke(proj2, *first2), invoke(proj1, *first1))) return false;
|
|
@@ -2007,23 +2020,20 @@ template<class InputIterator1, class InputIterator2, class Cmp>
|
|
| 2007 |
InputIterator2 b2, InputIterator2 e2,
|
| 2008 |
Cmp comp)
|
| 2009 |
-> decltype(comp(*b1, *b2));
|
| 2010 |
```
|
| 2011 |
|
|
|
|
|
|
|
|
|
|
| 2012 |
*Mandates:* `decltype(comp(*b1, *b2))` is a comparison category type.
|
| 2013 |
|
| 2014 |
-
*
|
| 2015 |
-
|
|
|
|
| 2016 |
|
| 2017 |
-
``
|
| 2018 |
-
for ( ; b1 != e1 && b2 != e2; void(++b1), void(++b2) )
|
| 2019 |
-
if (auto cmp = comp(*b1,*b2); cmp != 0)
|
| 2020 |
-
return cmp;
|
| 2021 |
-
return b1 != e1 ? strong_ordering::greater :
|
| 2022 |
-
b2 != e2 ? strong_ordering::less :
|
| 2023 |
-
strong_ordering::equal;
|
| 2024 |
-
```
|
| 2025 |
|
| 2026 |
``` cpp
|
| 2027 |
template<class InputIterator1, class InputIterator2>
|
| 2028 |
constexpr auto
|
| 2029 |
lexicographical_compare_three_way(InputIterator1 b1, InputIterator1 e1,
|
|
|
|
| 1 |
## Sorting and related operations <a id="alg.sorting">[[alg.sorting]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="alg.sorting.general">[[alg.sorting.general]]</a>
|
| 4 |
+
|
| 5 |
The operations in [[alg.sorting]] defined directly in namespace `std`
|
| 6 |
have two versions: one that takes a function object of type `Compare`
|
| 7 |
and one that uses an `operator<`.
|
| 8 |
|
| 9 |
`Compare` is a function object type [[function.objects]] that meets the
|
| 10 |
requirements for a template parameter named `BinaryPredicate`
|
| 11 |
[[algorithms.requirements]]. The return value of the function call
|
| 12 |
+
operation applied to an object of type `Compare`, when converted to
|
| 13 |
+
`bool`, yields `true` if the first argument of the call is less than the
|
| 14 |
+
second, and `false` otherwise. `Compare comp` is used throughout for
|
| 15 |
+
algorithms assuming an ordering relation.
|
| 16 |
|
| 17 |
For all algorithms that take `Compare`, there is a version that uses
|
| 18 |
`operator<` instead. That is, `comp(*i, *j) != false` defaults to
|
| 19 |
`*i < *j != false`. For algorithms other than those described in
|
| 20 |
[[alg.binary.search]], `comp` shall induce a strict weak ordering on the
|
|
|
|
| 50 |
bool(invoke(comp, invoke(proj, *(i + n)), invoke(proj, *i)))
|
| 51 |
```
|
| 52 |
|
| 53 |
is `false`.
|
| 54 |
|
| 55 |
+
A sequence is *sorted with respect to a comparator `comp`* for a
|
| 56 |
+
comparator `comp` if it is sorted with respect to `comp` and
|
| 57 |
+
`identity{}` (the identity projection).
|
| 58 |
+
|
| 59 |
A sequence \[`start`, `finish`) is *partitioned with respect to an
|
| 60 |
expression* `f(e)` if there exists an integer `n` such that for all
|
| 61 |
`0 <= i < (finish - start)`, `f(*(start + i))` is `true` if and only if
|
| 62 |
`i < n`.
|
| 63 |
|
|
|
|
| 289 |
`RandomAccessIterator` meets the *Cpp17ValueSwappable*
|
| 290 |
requirements [[swappable.requirements]], the type of `*result_first`
|
| 291 |
meets the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]]) and
|
| 292 |
*Cpp17MoveAssignable* ([[cpp17.moveassignable]]) requirements.
|
| 293 |
|
| 294 |
+
For iterators `a1` and `b1` in \[`first`, `last`), and iterators `x2`
|
| 295 |
+
and `y2` in \[`result_first`, `result_last`), after evaluating the
|
| 296 |
+
assignment `*y2 = *b1`, let E be the value of
|
| 297 |
|
| 298 |
``` cpp
|
| 299 |
bool(invoke(comp, invoke(proj1, *a1), invoke(proj2, *y2))).
|
| 300 |
```
|
| 301 |
|
|
|
|
| 473 |
return ranges::nth_element(ranges::begin(r), nth, ranges::end(r), comp, proj);
|
| 474 |
```
|
| 475 |
|
| 476 |
### Binary search <a id="alg.binary.search">[[alg.binary.search]]</a>
|
| 477 |
|
| 478 |
+
#### General <a id="alg.binary.search.general">[[alg.binary.search.general]]</a>
|
| 479 |
+
|
| 480 |
+
All of the algorithms in [[alg.binary.search]] are versions of binary
|
| 481 |
+
search and assume that the sequence being searched is partitioned with
|
| 482 |
+
respect to an expression formed by binding the search key to an argument
|
| 483 |
+
of the comparison function. They work on non-random access iterators
|
| 484 |
+
minimizing the number of comparisons, which will be logarithmic for all
|
| 485 |
+
types of iterators. They are especially appropriate for random access
|
| 486 |
+
iterators, because these algorithms do a logarithmic number of steps
|
| 487 |
+
through the data structure. For non-random access iterators they execute
|
| 488 |
+
a linear number of steps.
|
| 489 |
|
| 490 |
#### `lower_bound` <a id="lower.bound">[[lower.bound]]</a>
|
| 491 |
|
| 492 |
``` cpp
|
| 493 |
template<class ForwardIterator, class T>
|
|
|
|
| 767 |
- `i` for the overloads in namespace `std`.
|
| 768 |
- `{i, last}` for the overloads in namespace `ranges`.
|
| 769 |
|
| 770 |
*Complexity:* Let N = `last - first`:
|
| 771 |
|
| 772 |
+
- For the overloads with no `ExecutionPolicy`, at most N log₂ N swaps,
|
| 773 |
but only 𝑂(N) swaps if there is enough extra memory. Exactly N
|
| 774 |
applications of the predicate and projection.
|
| 775 |
- For the overload with an `ExecutionPolicy`, 𝑂(N log N) swaps and 𝑂(N)
|
| 776 |
applications of the predicate.
|
| 777 |
|
| 778 |
``` cpp
|
| 779 |
+
template<class InputIterator, class OutputIterator1, class OutputIterator2, class Predicate>
|
|
|
|
| 780 |
constexpr pair<OutputIterator1, OutputIterator2>
|
| 781 |
partition_copy(InputIterator first, InputIterator last,
|
| 782 |
OutputIterator1 out_true, OutputIterator2 out_false, Predicate pred);
|
| 783 |
template<class ExecutionPolicy, class ForwardIterator, class ForwardIterator1,
|
| 784 |
class ForwardIterator2, class Predicate>
|
|
|
|
| 809 |
`*first` is writable [[iterator.requirements.general]] to `out_true` and
|
| 810 |
`out_false`.
|
| 811 |
|
| 812 |
*Preconditions:* The input range and output ranges do not overlap.
|
| 813 |
|
| 814 |
+
[*Note 1*: For the overload with an `ExecutionPolicy`, there might be a
|
| 815 |
performance cost if `first`’s value type does not meet the
|
| 816 |
*Cpp17CopyConstructible* requirements. — *end note*]
|
| 817 |
|
| 818 |
*Effects:* For each iterator `i` in \[`first`, `last`), copies `*i` to
|
| 819 |
the output range beginning with `out_true` if E(`*i`) is `true`, or to
|
|
|
|
| 998 |
return ranges::inplace_merge(ranges::begin(r), middle, ranges::end(r), comp, proj);
|
| 999 |
```
|
| 1000 |
|
| 1001 |
### Set operations on sorted structures <a id="alg.set.operations">[[alg.set.operations]]</a>
|
| 1002 |
|
| 1003 |
+
#### General <a id="alg.set.operations.general">[[alg.set.operations.general]]</a>
|
| 1004 |
+
|
| 1005 |
+
Subclause [[alg.set.operations]] defines all the basic set operations on
|
| 1006 |
+
sorted structures. They also work with `multiset`s [[multiset]]
|
| 1007 |
+
containing multiple copies of equivalent elements. The semantics of the
|
| 1008 |
+
set operations are generalized to `multiset`s in a standard way by
|
| 1009 |
+
defining `set_union` to contain the maximum number of occurrences of
|
| 1010 |
+
every element, `set_intersection` to contain the minimum, and so on.
|
| 1011 |
|
| 1012 |
#### `includes` <a id="includes">[[includes]]</a>
|
| 1013 |
|
| 1014 |
``` cpp
|
| 1015 |
template<class InputIterator1, class InputIterator2>
|
|
|
|
| 1062 |
comparisons and applications of each projection.
|
| 1063 |
|
| 1064 |
#### `set_union` <a id="set.union">[[set.union]]</a>
|
| 1065 |
|
| 1066 |
``` cpp
|
| 1067 |
+
template<class InputIterator1, class InputIterator2, class OutputIterator>
|
|
|
|
| 1068 |
constexpr OutputIterator
|
| 1069 |
set_union(InputIterator1 first1, InputIterator1 last1,
|
| 1070 |
InputIterator2 first2, InputIterator2 last2,
|
| 1071 |
OutputIterator result);
|
| 1072 |
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
|
|
|
|
| 1075 |
set_union(ExecutionPolicy&& exec,
|
| 1076 |
ForwardIterator1 first1, ForwardIterator1 last1,
|
| 1077 |
ForwardIterator2 first2, ForwardIterator2 last2,
|
| 1078 |
ForwardIterator result);
|
| 1079 |
|
| 1080 |
+
template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
|
|
|
|
| 1081 |
constexpr OutputIterator
|
| 1082 |
set_union(InputIterator1 first1, InputIterator1 last1,
|
| 1083 |
InputIterator2 first2, InputIterator2 last2,
|
| 1084 |
OutputIterator result, Compare comp);
|
| 1085 |
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
|
|
|
|
| 1273 |
comparisons and applications of each projection.
|
| 1274 |
|
| 1275 |
*Remarks:* If \[`first1`, `last1`) contains m elements that are
|
| 1276 |
equivalent to each other and \[`first2`, `last2`) contains n elements
|
| 1277 |
that are equivalent to them, the last max(m - n, 0) elements from
|
| 1278 |
+
\[`first1`, `last1`) are copied to the output range, in order.
|
| 1279 |
|
| 1280 |
#### `set_symmetric_difference` <a id="set.symmetric.difference">[[set.symmetric.difference]]</a>
|
| 1281 |
|
| 1282 |
``` cpp
|
| 1283 |
template<class InputIterator1, class InputIterator2,
|
|
|
|
| 1356 |
elements from \[`first2`, `last2`) if m < n. In either case, the
|
| 1357 |
elements are copied in order.
|
| 1358 |
|
| 1359 |
### Heap operations <a id="alg.heap.operations">[[alg.heap.operations]]</a>
|
| 1360 |
|
| 1361 |
+
#### General <a id="alg.heap.operations.general">[[alg.heap.operations.general]]</a>
|
| 1362 |
+
|
| 1363 |
A random access range \[`a`, `b`) is a *heap with respect to `comp` and
|
| 1364 |
`proj`* for a comparator and projection `comp` and `proj` if its
|
| 1365 |
elements are organized such that:
|
| 1366 |
|
| 1367 |
- With `N = b - a`, for all i, 0 < i < N,
|
|
|
|
| 1398 |
|
| 1399 |
Let `comp` be `less{}` and `proj` be `identity{}` for the overloads with
|
| 1400 |
no parameters by those names.
|
| 1401 |
|
| 1402 |
*Preconditions:* The range \[`first`, `last - 1`) is a valid heap with
|
| 1403 |
+
respect to `comp` and `proj`. For the overloads in namespace `std`,
|
| 1404 |
+
`RandomAccessIterator` meets the *Cpp17ValueSwappable*
|
| 1405 |
+
requirements [[swappable.requirements]] and the type of `*first` meets
|
| 1406 |
+
the *Cpp17MoveConstructible* requirements ([[cpp17.moveconstructible]])
|
| 1407 |
+
and the *Cpp17MoveAssignable* requirements ([[cpp17.moveassignable]]).
|
| 1408 |
|
| 1409 |
*Effects:* Places the value in the location `last - 1` into the
|
| 1410 |
resulting heap \[`first`, `last`).
|
| 1411 |
|
| 1412 |
*Returns:* `last` for the overloads in namespace `ranges`.
|
|
|
|
| 1476 |
```
|
| 1477 |
|
| 1478 |
Let `comp` be `less{}` and `proj` be `identity{}` for the overloads with
|
| 1479 |
no parameters by those names.
|
| 1480 |
|
| 1481 |
+
*Preconditions:* For the overloads in namespace `std`,
|
| 1482 |
+
`RandomAccessIterator` meets the *Cpp17ValueSwappable*
|
| 1483 |
+
requirements [[swappable.requirements]] and the type of `*first` meets
|
| 1484 |
+
the *Cpp17MoveConstructible* ([[cpp17.moveconstructible]]) and
|
| 1485 |
+
*Cpp17MoveAssignable* ([[cpp17.moveassignable]]) requirements.
|
| 1486 |
|
| 1487 |
*Effects:* Constructs a heap with respect to `comp` and `proj` out of
|
| 1488 |
the range \[`first`, `last`).
|
| 1489 |
|
| 1490 |
*Returns:* `last` for the overloads in namespace `ranges`.
|
|
|
|
| 1636 |
```
|
| 1637 |
|
| 1638 |
*Preconditions:* For the first form, `T` meets the
|
| 1639 |
*Cpp17LessThanComparable* requirements ([[cpp17.lessthancomparable]]).
|
| 1640 |
|
| 1641 |
+
*Returns:* The smaller value. Returns the first argument when the
|
| 1642 |
+
arguments are equivalent.
|
|
|
|
|
|
|
|
|
|
| 1643 |
|
| 1644 |
*Complexity:* Exactly one comparison and two applications of the
|
| 1645 |
projection, if any.
|
| 1646 |
|
| 1647 |
+
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1648 |
+
template parameter `T` of the overloads in namespace `std`.
|
| 1649 |
+
|
| 1650 |
``` cpp
|
| 1651 |
template<class T>
|
| 1652 |
constexpr T min(initializer_list<T> r);
|
| 1653 |
template<class T, class Compare>
|
| 1654 |
constexpr T min(initializer_list<T> r, Compare comp);
|
|
|
|
| 1666 |
*Preconditions:* `ranges::distance(r) > 0`. For the overloads in
|
| 1667 |
namespace `std`, `T` meets the *Cpp17CopyConstructible* requirements.
|
| 1668 |
For the first form, `T` meets the *Cpp17LessThanComparable* requirements
|
| 1669 |
([[cpp17.lessthancomparable]]).
|
| 1670 |
|
| 1671 |
+
*Returns:* The smallest value in the input range. Returns a copy of the
|
| 1672 |
+
leftmost element when several elements are equivalent to the smallest.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1673 |
|
| 1674 |
*Complexity:* Exactly `ranges::distance(r) - 1` comparisons and twice as
|
| 1675 |
many applications of the projection, if any.
|
| 1676 |
|
| 1677 |
+
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1678 |
+
template parameter `T` of the overloads in namespace `std`.
|
| 1679 |
+
|
| 1680 |
``` cpp
|
| 1681 |
template<class T>
|
| 1682 |
constexpr const T& max(const T& a, const T& b);
|
| 1683 |
template<class T, class Compare>
|
| 1684 |
constexpr const T& max(const T& a, const T& b, Compare comp);
|
|
|
|
| 1689 |
```
|
| 1690 |
|
| 1691 |
*Preconditions:* For the first form, `T` meets the
|
| 1692 |
*Cpp17LessThanComparable* requirements ([[cpp17.lessthancomparable]]).
|
| 1693 |
|
| 1694 |
+
*Returns:* The larger value. Returns the first argument when the
|
| 1695 |
+
arguments are equivalent.
|
|
|
|
|
|
|
|
|
|
| 1696 |
|
| 1697 |
*Complexity:* Exactly one comparison and two applications of the
|
| 1698 |
projection, if any.
|
| 1699 |
|
| 1700 |
+
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1701 |
+
template parameter `T` of the overloads in namespace `std`.
|
| 1702 |
+
|
| 1703 |
``` cpp
|
| 1704 |
template<class T>
|
| 1705 |
constexpr T max(initializer_list<T> r);
|
| 1706 |
template<class T, class Compare>
|
| 1707 |
constexpr T max(initializer_list<T> r, Compare comp);
|
|
|
|
| 1719 |
*Preconditions:* `ranges::distance(r) > 0`. For the overloads in
|
| 1720 |
namespace `std`, `T` meets the *Cpp17CopyConstructible* requirements.
|
| 1721 |
For the first form, `T` meets the *Cpp17LessThanComparable* requirements
|
| 1722 |
([[cpp17.lessthancomparable]]).
|
| 1723 |
|
| 1724 |
+
*Returns:* The largest value in the input range. Returns a copy of the
|
| 1725 |
+
leftmost element when several elements are equivalent to the largest.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1726 |
|
| 1727 |
*Complexity:* Exactly `ranges::distance(r) - 1` comparisons and twice as
|
| 1728 |
many applications of the projection, if any.
|
| 1729 |
|
| 1730 |
+
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1731 |
+
template parameter `T` of the overloads in namespace `std`.
|
| 1732 |
+
|
| 1733 |
``` cpp
|
| 1734 |
template<class T>
|
| 1735 |
constexpr pair<const T&, const T&> minmax(const T& a, const T& b);
|
| 1736 |
template<class T, class Compare>
|
| 1737 |
constexpr pair<const T&, const T&> minmax(const T& a, const T& b, Compare comp);
|
|
|
|
| 1745 |
*Preconditions:* For the first form, `T` meets the
|
| 1746 |
*Cpp17LessThanComparable* requirements ([[cpp17.lessthancomparable]]).
|
| 1747 |
|
| 1748 |
*Returns:* `{b, a}` if `b` is smaller than `a`, and `{a, b}` otherwise.
|
| 1749 |
|
|
|
|
|
|
|
|
|
|
| 1750 |
*Complexity:* Exactly one comparison and two applications of the
|
| 1751 |
projection, if any.
|
| 1752 |
|
| 1753 |
+
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1754 |
+
template parameter `T` of the overloads in namespace `std`.
|
| 1755 |
+
|
| 1756 |
``` cpp
|
| 1757 |
template<class T>
|
| 1758 |
constexpr pair<T, T> minmax(initializer_list<T> t);
|
| 1759 |
template<class T, class Compare>
|
| 1760 |
constexpr pair<T, T> minmax(initializer_list<T> t, Compare comp);
|
|
|
|
| 1777 |
|
| 1778 |
*Returns:* Let `X` be the return type. Returns `X{x, y}`, where `x` is a
|
| 1779 |
copy of the leftmost element with the smallest value and `y` a copy of
|
| 1780 |
the rightmost element with the largest value in the input range.
|
| 1781 |
|
|
|
|
|
|
|
|
|
|
| 1782 |
*Complexity:* At most (3/2)`ranges::distance(r)` applications of the
|
| 1783 |
corresponding predicate and twice as many applications of the
|
| 1784 |
projection, if any.
|
| 1785 |
|
| 1786 |
+
*Remarks:* An invocation may explicitly specify an argument for the
|
| 1787 |
+
template parameter `T` of the overloads in namespace `std`.
|
| 1788 |
+
|
| 1789 |
``` cpp
|
| 1790 |
template<class ForwardIterator>
|
| 1791 |
constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
|
| 1792 |
|
| 1793 |
template<class ExecutionPolicy, class ForwardIterator>
|
|
|
|
| 1797 |
template<class ForwardIterator, class Compare>
|
| 1798 |
constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
|
| 1799 |
Compare comp);
|
| 1800 |
template<class ExecutionPolicy, class ForwardIterator, class Compare>
|
| 1801 |
ForwardIterator min_element(ExecutionPolicy&& exec,
|
| 1802 |
+
ForwardIterator first, ForwardIterator last, Compare comp);
|
|
|
|
| 1803 |
|
| 1804 |
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
|
| 1805 |
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
|
| 1806 |
constexpr I ranges::min_element(I first, S last, Comp comp = {}, Proj proj = {});
|
| 1807 |
template<forward_range R, class Proj = identity,
|
|
|
|
| 1881 |
minmax_element(ExecutionPolicy&& exec,
|
| 1882 |
ForwardIterator first, ForwardIterator last, Compare comp);
|
| 1883 |
|
| 1884 |
template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
|
| 1885 |
indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>
|
| 1886 |
+
constexpr ranges::minmax_element_result<I>
|
| 1887 |
ranges::minmax_element(I first, S last, Comp comp = {}, Proj proj = {});
|
| 1888 |
template<forward_range R, class Proj = identity,
|
| 1889 |
indirect_strict_weak_order<projected<iterator_t<R>, Proj>> Comp = ranges::less>
|
| 1890 |
+
constexpr ranges::minmax_element_result<borrowed_iterator_t<R>>
|
| 1891 |
ranges::minmax_element(R&& r, Comp comp = {}, Proj proj = {});
|
| 1892 |
```
|
| 1893 |
|
| 1894 |
*Returns:* `{first, first}` if \[`first`, `last`) is empty, otherwise
|
| 1895 |
`{m, M}`, where `m` is the first iterator in \[`first`, `last`) such
|
| 1896 |
that no iterator in the range refers to a smaller element, and where `M`
|
| 1897 |
+
is the last iterator[^5]
|
| 1898 |
+
|
| 1899 |
+
in \[`first`, `last`) such that no iterator in the range refers to a
|
| 1900 |
+
larger element.
|
| 1901 |
|
| 1902 |
*Complexity:* Let N be `last - first`. At most
|
| 1903 |
$\max(\bigl\lfloor{\frac{3}{2}} (N-1)\bigr\rfloor, 0)$ comparisons and
|
| 1904 |
twice as many applications of the projection, if any.
|
| 1905 |
|
|
|
|
| 1917 |
```
|
| 1918 |
|
| 1919 |
Let `comp` be `less{}` for the overloads with no parameter `comp`, and
|
| 1920 |
let `proj` be `identity{}` for the overloads with no parameter `proj`.
|
| 1921 |
|
| 1922 |
+
*Preconditions:*
|
| 1923 |
+
`bool(invoke(comp, invoke(proj, hi), invoke(proj, lo)))` is `false`. For
|
| 1924 |
+
the first form, type `T` meets the *Cpp17LessThanComparable*
|
| 1925 |
+
requirements ([[cpp17.lessthancomparable]]).
|
| 1926 |
|
| 1927 |
+
*Returns:* `lo` if
|
| 1928 |
+
`bool(invoke(comp, invoke(proj, v), invoke(proj, lo)))` is `true`, `hi`
|
| 1929 |
+
if `bool(invoke(comp, invoke(proj, hi), invoke(proj, v)))` is `true`,
|
| 1930 |
+
otherwise `v`.
|
| 1931 |
|
| 1932 |
[*Note 1*: If NaN is avoided, `T` can be a floating-point
|
| 1933 |
type. — *end note*]
|
| 1934 |
|
| 1935 |
*Complexity:* At most two comparisons and three applications of the
|
|
|
|
| 1994 |
corresponding pair of elements that are not equivalent.
|
| 1995 |
|
| 1996 |
[*Example 1*:
|
| 1997 |
|
| 1998 |
`ranges::lexicographical_compare(I1, S1, I2, S2, Comp, Proj1, Proj2)`
|
| 1999 |
+
can be implemented as:
|
| 2000 |
|
| 2001 |
``` cpp
|
| 2002 |
for ( ; first1 != last1 && first2 != last2 ; ++first1, (void) ++first2) {
|
| 2003 |
if (invoke(comp, invoke(proj1, *first1), invoke(proj2, *first2))) return true;
|
| 2004 |
if (invoke(comp, invoke(proj2, *first2), invoke(proj1, *first1))) return false;
|
|
|
|
| 2020 |
InputIterator2 b2, InputIterator2 e2,
|
| 2021 |
Cmp comp)
|
| 2022 |
-> decltype(comp(*b1, *b2));
|
| 2023 |
```
|
| 2024 |
|
| 2025 |
+
Let N be min(`e1 - b1`, `e2 - b2`). Let E(n) be
|
| 2026 |
+
`comp(*(b1 + `n`), *(b2 + `n`))`.
|
| 2027 |
+
|
| 2028 |
*Mandates:* `decltype(comp(*b1, *b2))` is a comparison category type.
|
| 2029 |
|
| 2030 |
+
*Returns:* E(i), where i is the smallest integer in \[`0`, N) such that
|
| 2031 |
+
E(i)` != 0` is `true`, or `(e1 - b1) <=> (e2 - b2)` if no such integer
|
| 2032 |
+
exists.
|
| 2033 |
|
| 2034 |
+
*Complexity:* At most N applications of `comp`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2035 |
|
| 2036 |
``` cpp
|
| 2037 |
template<class InputIterator1, class InputIterator2>
|
| 2038 |
constexpr auto
|
| 2039 |
lexicographical_compare_three_way(InputIterator1 b1, InputIterator1 e1,
|