Namespaces
Variants

std:: is_sorted_until

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
(C++11)
is_sorted_until
(C++11)

Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <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 起)

检查范围 [ first , last ) ,并找出从 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 - 比较函数对象(即满足比较要求对象),若第一参数小于(即先序于)第二参数则返回 ​ true

比较函数的签名应等价于如下形式:

bool cmp ( const Type1 & a, const Type2 & b ) ;

虽然签名不必包含 const & ,函数也不能修改传递给它的对象,而且必须能够接受(可能为 const 的)类型 Type1 Type2 的所有值,与值类别无关(从而不允许 Type1& ,也不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起) )。
类型 Type1 Type2 必须使得 ForwardIt 类型的对象能在解引用后隐式转换到这两个类型。 ​

类型要求
-
ForwardIt 必须满足 前向迭代器 的要求。
-
Compare 必须满足 比较 的要求。

返回值

起始于 first 的最大有序范围的上界,该范围内的元素按升序排列。即满足范围 [ first , it ) 为有序的最后一个迭代器 it

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

复杂度

给定 N std:: 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 个初始有序元素
^
4 5 9 3 1 1 : 3 个初始有序元素
^^^^^
9 3 1 4 5 1 : 1 个初始有序元素
^
1 3 5 4 1 9 : 3 个初始有序元素
^^^^^
5 9 1 1 3 4 : 2 个初始有序元素
^^^
4 9 1 5 1 3 : 2 个初始有序元素
^^^
1 1 4 9 5 3 : 4 个初始有序元素
^^^^^^^

参见

(C++11)
检查范围是否按升序排序
(函数模板)
查找最大已排序子范围
(算法函数对象)