命名空间
变体
操作

std::is_sorted_until

来自 cppreference.cn
< cpp‎ | 算法
 
 
算法库
有约束算法与针对范围的算法 (C++20)
有约束的算法,例如 ranges::copyranges::sort 等……
执行策略 (C++17)
排序及相关操作
划分操作
排序操作
(C++11)
is_sorted_until
(C++11)

二分搜索操作
(于已划分范围上)
集合操作(于已排序范围上)
归并操作(于已排序范围上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库
数值操作
未初始化内存上的操作
 
定义于头文件 <algorithm>
template< class ForwardIt >
ForwardIt is_sorted_until( ForwardIt first, ForwardIt last );
(1) (C++11 起)
(C++20 起为 constexpr)
template< class ExecutionPolicy, class ForwardIt >

ForwardIt is_sorted_until( ExecutionPolicy&& policy,

                           ForwardIt first, ForwardIt last );
(2) (C++17 起)
template< class ForwardIt, class Compare >

ForwardIt is_sorted_until( ForwardIt first, ForwardIt last,

                           Compare comp );
(3) (C++11 起)
(C++20 起为 constexpr)
template< class ExecutionPolicy, class ForwardIt, class Compare >

ForwardIt is_sorted_until( ExecutionPolicy&& policy,
                           ForwardIt first, ForwardIt last,

                           Compare comp );
(4) (C++17 起)

检查范围 [firstlast),并查找从 first 开始的、元素按非降序排列的最大范围。

1) 查找元素相对于 operator<(C++20 前)std::less{}(C++20 起) 排序的最大范围。
3) 查找元素相对于 comp 排序的最大范围。
2,4)(1,3),但按 policy 执行。
仅当满足所有以下条件时,这些重载才参与重载决议

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&,但函数不得修改传递给它的对象,并且必须能够接受 Type1Type2 类型的所有值(可能为 const),无论其 值类别 (因此,不允许使用 Type1&;除非对于 Type1,移动等同于复制,否则也不允许使用 Type1(C++11 起))。
类型 Type1Type2 必须使得类型 ForwardIt 的对象可以被解引用,然后隐式转换为两者。​

类型要求
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。
-
Compare 必须满足 Compare 的要求。

[编辑] 返回值

first 开始的,元素按升序排列的最大范围的上界。即,范围 [firstit) 已排序的最后一个迭代器 it

对于空范围和长度为一的范围,返回 last

[编辑] 复杂度

给定 Nstd::distance(first, last)

1,2) O(N) 次比较,使用 operator<(C++20 前)std::less{}(C++20 起)
3,4) O(N) 次比较器 comp 的应用。

[编辑] 异常

带有模板参数 ExecutionPolicy 的重载按如下方式报告错误

  • 若算法部分调用的函数抛出异常,且 ExecutionPolicy标准策略 之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的。
  • 如果算法未能分配内存,则抛出 std::bad_alloc

[编辑] 可能的实现

亦见 libstdc++ 和 libc++ 中的实现。

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)
检查一个范围是否按升序排序
(函数模板) [编辑]
寻找最大的已排序子范围
(算法函数对象)[编辑]