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 の要件を満たすオブジェクト)。最初の引数が2番目の引数より小さい(つまり、前に順序付けられる)場合に true を返します。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャは const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、値カテゴリ に関係なく、(おそらく const の) |
| 型要件 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-CompareはCompareの要件を満たす必要がある。 | ||
[編集] 戻り値
first から始まる、要素が昇順にソートされている最大の範囲の上限。つまり、範囲 [first, it) がソートされている最後のイテレータ it。
空の範囲と長さ1の範囲に対しては last を返します。
[編集] 計算量
std::distance(first, last) を N とする
[編集] 例外
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) |
ソートされている最大のサブ範囲を見つける (アルゴリズム関数オブジェクト) |