名前空間
変種

「cpp/algorithm/is sorted」の版間の差分

提供: cppreference.com
Fix some translations
Milkpot (トーク | 投稿記録)
編集の要約なし
 
(同じ利用者による、間の3版が非表示)
1行目: 1行目:
{{tr_note}}
{{cpp/title|is_sorted}}
{{cpp/title|is_sorted}}
{{cpp/algorithm/navbar}}
{{cpp/algorithm/navbar}}
{{dcl begin}}
{{dcl begin}}
{{dcl header | algorithm}}
{{dcl header | algorithm}}
{{dcl | num=1 | since=c++11 |
{{dcl | num=1 | =c++11 |
template< class ForwardIt >
template< class ForwardIt >
bool is_sorted( ForwardIt first, ForwardIt last );
bool is_sorted( ForwardIt first, ForwardIt last );
}}
}}
{{dcl | num=2 | since=c++11 |
{{dcl | num=2 |
=c++11 |
template< class ForwardIt, class Compare >
template< class ForwardIt, class Compare >
bool is_sorted( ForwardIt first, ForwardIt last, Compare comp );
bool is_sorted( ForwardIt first, ForwardIt last, Compare comp );
}}
}}
{{dcl end}}
{{dcl end}}


Checks if the elements in range {{tt|[first, last)}} are sorted in ascending order. The first version of the function uses {{c|operator<}} to compare the elements, the second uses the given comparison function {{tt|comp}}.
{{tt|[first, last)}}
===パラメータ===
 
{{|operator<}}
,{{tt|}}
 
======
{{par begin}}
{{par begin}}
{{par | first, last |{{tr| 検討する要素の範囲| the range of elements to examine}}}}
{{par | first, last |
{{par cmp | comp | p1=ForwardIt}}
{{}}
{{par cmp | comp | p1=ForwardIt}}
{{par hreq}}
{{par hreq}}
{{par req concept | ForwardIt | ForwardIterator}}
{{par req | ForwardIt | ForwardIterator}}
{{par end}}
{{par end}}


===値を返します===
======
{{c|true}} if the elements in the range are sorted in ascending order
 
{{c|true}}
 
 


===複雑性===
======
{{tr|{{tt|first}}{{tt|last}}との間の距離の線形|linear in the distance between {{tt|first}} and {{tt|last}}}}
{{|}}


===可能な実装===
======
{{eq fun
{{eq fun
  | 1=
  | 1=
44行目: 68行目:
}
}
}}
}}


===例===
===例===
50行目: 77行目:
#include <iostream>
#include <iostream>
#include <algorithm>
#include <algorithm>
 
int main()  
int main()  
{
{
    const int N = 5;
     int digits[] = {3, 1, 4, 1, 5};
     int digits[N] = {3, 1, 4, 1, 5};


     for (auto i : digits) std::cout << i << ' ';
     for (auto i : digits) std::cout << i << ' ';
     std::cout << ": is_sorted: " << std::is_sorted(digits, digits+N) << '\n';
     std::cout << ": is_sorted: "
<< std::is_sorted(digits, digits) << '\n';


     std::sort(digits, digits+N);
     std::sort(digits, digits);


     for (auto i : digits) std::cout << i << ' ';
     for (auto i : digits) std::cout << i << ' ';
     std::cout << ": is_sorted: " << std::is_sorted(digits, digits+N) << '\n';
     std::cout << ": is_sorted: "
<< std::is_sorted(digits, digits) << '\n';
}
}
  | output=
  | output=
3 1 4 1 5 : is_sorted: 0
3 1 4 1 5 : is_sorted:
1 1 3 4 5 : is_sorted: 1
1 1 3 4 5 : is_sorted:
}}
}}


===参照===
======
{{dsc begin}}
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc is_sorted_until}}
{{dsc inc | cpp/algorithm/dsc is_sorted_until}}
{{dsc end}}
{{dsc end}}


[[de:cpp/algorithm/is sorted]]
deenesfritptruzh
[[en:cpp/algorithm/is sorted]]
[[es:cpp/algorithm/is sorted]]
[[fr:cpp/algorithm/is sorted]]
[[it:cpp/algorithm/is sorted]]
[[pt:cpp/algorithm/is sorted]]
[[ru:cpp/algorithm/is sorted]]
[[zh:cpp/algorithm/is sorted]]

2019年12月13日 (金) 10:59時点における最新版

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

順列
数値演算
C のライブラリ
 
<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 > bool is_sorted( ForwardIt first, ForwardIt last );
(C++11以上)
(C++20未満)
template< class ForwardIt > constexpr bool is_sorted( ForwardIt first, ForwardIt last );
(C++20以上)
template< class ExecutionPolicy, class ForwardIt > bool is_sorted( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );
(2) (C++17以上)
(3)
template< class ForwardIt, class Compare > bool is_sorted( ForwardIt first, ForwardIt last, Compare comp );
(C++11以上)
(C++20未満)
template< class ForwardIt, class Compare > constexpr bool is_sorted( ForwardIt first, ForwardIt last, Compare comp );
(C++20以上)
template< class ExecutionPolicy, class ForwardIt, class Compare > bool is_sorted( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Compare comp );
(4) (C++17以上)

範囲 [first, last) の要素が非降順にソートされているかどうか調べます。

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 の要件を満たさなければなりません。

戻り値

範囲内の要素が昇順にソートされていれば true

計算量

firstlast の距離に比例。

例外

テンプレート引数 ExecutionPolicy を持つオーバーロードは以下のようにエラーを報告します。

  • アルゴリズムの一部として呼び出された関数の実行が例外を投げ、 ExecutionPolicy標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆる ExecutionPolicy については、動作は処理系定義です。
  • アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。

実装例

1つめのバージョン
template<class ForwardIt>
bool is_sorted(ForwardIt first, ForwardIt last)
{
    return std::is_sorted_until(first, last) == last;
}
2つめのバージョン
template<class ForwardIt, class Compare>
bool is_sorted(ForwardIt first, ForwardIt last, Compare comp)
{
    return std::is_sorted_until(first, last, comp) == last;
}

ノート

std::is_sorted は空の範囲および長さ1の範囲に対して true を返します。

#include <iostream>
#include <algorithm>
#include <iterator>
int main() 
{
    int digits[] = {3, 1, 4, 1, 5};

    for (auto i : digits) std::cout << i << ' ';
    std::cout << ": is_sorted: " << std::boolalpha
              << std::is_sorted(std::begin(digits), std::end(digits)) << '\n';

    std::sort(std::begin(digits), std::end(digits));

    for (auto i : digits) std::cout << i << ' ';
    std::cout << ": is_sorted: "
              << std::is_sorted(std::begin(digits), std::end(digits)) << '\n';
}

出力:

3 1 4 1 5 : is_sorted: false
1 1 3 4 5 : is_sorted: true

関連項目

最も大きなソート済みの部分範囲を探します
(関数テンプレート) [edit]