名前空間
変種
操作

std::is_sorted_until

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
コンセプトとユーティリティ: std::sortable, std::projected, ...
制約付きアルゴリズム: std::ranges::copy, std::ranges::sort, ...
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
(C++11)
is_sorted_until
(C++11)
二分探索操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)

順列
数値演算
C のライブラリ
 
ヘッダ <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 の要件を満たすオブジェクト)。

比較関数のシグネチャは以下と同等なものであるべきです。

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

シグネチャが const & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、 Type1 型および Type2 型 (およびそれらの const 修飾された型) のすべての値を値カテゴリに関わらず受理できなければなりません (そのため Type1 & は許されません。 また Type1 に対してムーブがコピーと同等でなければ Type1 も許されません (C++11以上))。
Type1 および Type2 は、どちらも ForwardIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

型の要件
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。

[編集] 戻り値

要素が昇順にソートされている first から始まる最も大きな範囲の上限。 つまり、範囲 [first, it) がソートされている最後のイテレータ it

[編集] 計算量

firstlast の距離に比例。

[編集] 例外

テンプレート引数 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 を返します。

[編集]

#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)
指定範囲が昇順にソートされているか調べます
(関数テンプレート) [edit]