std::is_sorted_until
定義於標頭檔案 <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, |
(2) | (C++17 起) |
template< class ForwardIt, class Compare > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, |
(3) | (C++11 起) (C++20 起為 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class Compare > ForwardIt is_sorted_until( ExecutionPolicy&& policy, |
(4) | (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。
對於空範圍和長度為一的範圍,返回 last。
[編輯] 複雜度
給定 N 為 std::distance(first, last)
[編輯] 異常
帶有模板引數 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) |
檢查一個範圍是否按升序排序 (函式模板) |
(C++20) |
尋找最大的已排序子範圍 (演算法函式物件) |