名稱空間
變體
操作

std::is_sorted_until

來自 cppreference.com
< 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)
檢查一個範圍是否按升序排序
(函式模板) [編輯]
尋找最大的已排序子範圍
(演算法函式物件)[編輯]