std::is_sorted_until
定义在头文件 <algorithm> |
||
template< class ForwardIt > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last ); |
(1) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt > ForwardIt is_sorted_until( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class ForwardIt, class Compare > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, |
(3) | (since C++11) (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt, class Compare > ForwardIt is_sorted_until( ExecutionPolicy&& policy, |
(4) | (since C++17) |
检查范围 [
first,
last)
并找到最大的范围,该范围从 first 开始,其中元素以非降序排序。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(直到 C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(自 C++20 起) |
目录 |
[编辑] 参数
first, last | - | 定义要检查的元素范围的迭代器对 |
policy | - | 要使用的执行策略 |
comp | - | 比较函数对象(即满足Compare要求的对象),如果第一个参数小于(即排序在之前)第二个参数,则返回 true。 比较函数的签名应等效于以下内容 bool cmp(const Type1& a, const Type2& b); 虽然签名不需要有 const&,但函数不得修改传递给它的对象,并且必须能够接受类型为 |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-Compare 必须满足 Compare 的要求。 |
[编辑] 返回值
以 first 开头的最大范围的上界,其中元素按升序排序。也就是说,范围 [
first,
it)
排序的最后一个迭代器 it。
对于空范围和长度为 1 的范围,返回 last。
[编辑] 复杂度
给定 N 为 std::distance(first, last)
[编辑] 异常
具有名为 ExecutionPolicy
的模板参数的重载按如下方式报告错误
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为都是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
is_sorted_until (1) |
---|
template<class ForwardIt> constexpr //< since C++20 ForwardIt is_sorted_until(ForwardIt first, ForwardIt last) { return std::is_sorted_until(first, last, std::less<>()); } |
is_sorted_until (2) |
template<class ForwardIt, class Compare> constexpr //< since C++20 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; } |
[编辑] 示例
#include <algorithm> #include <cassert> #include <iostream> #include <iterator> #include <random> #include <string> 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; for (int sorted_size = 0; sorted_size < min_sorted_size;) { std::shuffle(nums, nums + N, g); int *const sorted_end = std::is_sorted_until(nums, nums + N); sorted_size = std::distance(nums, sorted_end); assert(sorted_size >= 1); for (const auto i : nums) std::cout << i << ' '; std::cout << ": " << sorted_size << " initial sorted elements\n" << std::string(sorted_size * 2 - 1, '^') << '\n'; } }
可能的输出
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) |
检查范围是否已排序为升序 (函数模板) |
(C++20) |
查找最大的已排序子范围 (算法函数对象) |