std::is_sorted_until
受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C 库 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
在标头
<algorithm>
定义
|
||
(1) | ||
template< class ForwardIt >
ForwardIt is_sorted_until( ForwardIt first, ForwardIt last ); |
(C++11 起) (C++20 前) |
|
template< class ForwardIt >
constexpr ForwardIt is_sorted_until( ForwardIt first, ForwardIt last ); |
(C++20 起) | |
template< class ExecutionPolicy,
class ForwardIt >
ForwardIt is_sorted_until( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
(3) | ||
template< class ForwardIt, class Compare >
ForwardIt is_sorted_until( ForwardIt first,
ForwardIt last, |
(C++11 起) (C++20 前) |
|
template< class ForwardIt, class Compare >
constexpr ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, |
(C++20 起) | |
template< class ExecutionPolicy,
class ForwardIt, class
Compare >
ForwardIt is_sorted_until( ExecutionPolicy&& policy, |
(4) | (C++17 起) |
检验范围 [first, last)
,并寻找始于 first
且其中元素已以不降序排序的最大范围。
若对于任何指向序列的迭代器 it
与任何使得 it + n
为指向序列元素的合法迭代器的非负整数 n
, comp(*(it + n), *it) 求值为 false
,则称序列相对于 comp
已排序。
operator<
比较元素。comp
比较元素。policy
执行。这些重载只有在
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> |
(C++20 起) |
参数
first, last | - | 要检验的元素范围 |
policy | - | 所用的执行策略。细节见执行策略。 |
comp | - | 比较函数对象(即满足比较 (Compare)
概念的对象),在第一参数小于(即先 序于)第二参数时返回 true。 比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 |
类型要求 | ||
-ForwardIt 必须符合老式向前迭代器 (LegacyForwardIterator)
的要求。
|
返回值
始于 first
且其中元素已以升序排序的最大范围。即满足范围 [first, it)
已排序的最后迭代器 it
。
复杂度
与 first
和 last
间的距离成线性。
异常
拥有名为 ExecutionPolicy
的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy
是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy
,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
版本一 |
---|
template<class ForwardIt> ForwardIt is_sorted_until(ForwardIt first, ForwardIt last) { return is_sorted_until(first, last, std::less<>()); } |
版本二 |
template <class ForwardIt, class Compare> ForwardIt is_sorted_until(ForwardIt first, ForwardIt last, Compare comp) { if (first != last) { ForwardIt next = first; while (++next != last) { if (comp(*next, *first)) return next; first = next; } } return last; } |
注解
std::is_sorted_until
对空范围及长为 1 的范围均返回 last
。
示例
#include <iostream> #include <algorithm> #include <iterator> #include <random> int main() { std::random_device rd; std::mt19937 g(rd()); const int N = 6; int nums[N] = {3, 1, 4, 1, 5, 9}; const int min_sorted_size = 4; int sorted_size = 0; do { std::shuffle(nums, nums + N, g); int *sorted_end = std::is_sorted_until(nums, nums + N); sorted_size = std::distance(nums, sorted_end); for (auto i : nums) std::cout << i << ' '; std::cout << " : " << sorted_size << " initial sorted elements\n"; } while (sorted_size < min_sorted_size); }
可能的输出:
4 1 9 5 1 3 : 1 initial sorted elements 4 5 9 3 1 1 : 3 initial sorted elements 9 3 1 4 5 1 : 1 initial sorted elements 1 3 5 4 1 9 : 3 initial sorted elements 5 9 1 1 3 4 : 2 initial sorted elements 4 9 1 5 1 3 : 2 initial sorted elements 1 1 4 9 5 3 : 4 initial sorted elements
参阅
(C++11)
|
检查范围是否已按升序排列 (函数模板) |