std::is_sorted_until
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| ヘッダ <algorithm> で定義
|
||
| (1) | ||
template< class ForwardIt > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last ); |
(C++11以上) (C++20未満) |
|
template< class ForwardIt > constexpr ForwardIt is_sorted_until( ForwardIt first, ForwardIt last ); |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt > ForwardIt is_sorted_until( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last ); |
(2) | (C++17以上) |
| (3) | ||
template< class ForwardIt, class Compare > ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, Compare comp ); |
(C++11以上) (C++20未満) |
|
template< class ForwardIt, class Compare > constexpr ForwardIt is_sorted_until( ForwardIt first, ForwardIt last, Compare comp ); |
(C++20以上) | |
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< を用いて比較されます。3) 要素は指定された二項比較関数
comp を用いて比較されます。2,4) (1,3) と同じですが、
policy に従って実行されます。 これらのオーバーロードは、 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true でなければ、オーバーロード解決に参加しません。引数
| first, last | - | 調べる要素の範囲 |
| policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
| comp | - | 最初の要素が2番目の要素より小さい (前に順序づけられる) 場合に true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。比較関数のシグネチャは以下と同等なものであるべきです。
シグネチャが |
| 型の要件 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
| ||
戻り値
要素が昇順にソートされている first から始まる最も大きな範囲の上限。 つまり、範囲 [first, it) がソートされている最後のイテレータ it。
計算量
first と last の距離に比例。
例外
テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicyが標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicyについては、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
実装例
| 1つめのバージョン |
|---|
template<class ForwardIt>
ForwardIt is_sorted_until(ForwardIt first, ForwardIt last)
{
return is_sorted_until(first, last, std::less<>());
}
|
| 2つめのバージョン |
template <class ForwardIt, class Compare>
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;
}
|
ノート
std::is_sorted_until は空の範囲および長さ1の範囲に対して last を返します。
例
Run this code
#include <iostream>
#include <algorithm>
#include <iterator>
#include <random>
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;
int sorted_size = 0;
do {
std::shuffle(nums, nums + N, g);
int *sorted_end = std::is_sorted_until(nums, nums + N);
sorted_size = std::distance(nums, sorted_end);
for (auto i : nums) std::cout << i << ' ';
std::cout << " : " << sorted_size << " initial sorted elements\n";
} while (sorted_size < min_sorted_size);
}
出力例:
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) |
指定範囲が昇順にソートされているか調べます (関数テンプレート) |