std::is_sorted_until
| 定义于头文件 <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 起) 为 true 才参与重载决议。参数
| first, last | - | 要检验的元素范围 |
| policy | - | 所用的执行策略。细节见执行策略。 |
| comp | - | 比较函数对象(即满足比较 (Compare) 概念的对象),若第一参数小于(即先序于)第二参数则返回 true 。 比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
| 类型要求 | ||
-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) |
检查范围是否已按升序排列 (函数模板) |