std::sort
| ヘッダー <algorithm> で定義 |
||
| template< class RandomIt > void sort( RandomIt first, RandomIt last ); |
(1) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class RandomIt > void sort( ExecutionPolicy&& policy, |
(2) | (C++17以降) |
| template< class RandomIt, class Compare > void sort( RandomIt first, RandomIt last, Compare comp ); |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class RandomIt, class Compare > void sort( ExecutionPolicy&& policy, |
(4) | (C++17以降) |
範囲 [first, last) 内の要素を非降順にソートします。等しい要素の順序は保持される保証はありません。
|
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以降) |
次のいずれかの条件が満たされる場合、動作は未定義です。
|
(C++11まで) |
|
(C++11以降) |
目次 |
[編集] パラメータ
| first, last | - | ソートする要素の 範囲 を定義するイテレータのペア |
| policy | - | 使用する 実行ポリシー |
| comp | - | 比較関数オブジェクト(つまり、Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より小さい(つまり、前に順序付けられる)場合に true を返します。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャは const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、値カテゴリ に関係なく、(おそらく const の) |
| 型要件 | ||
-RandomItはLegacyRandomAccessIteratorの要件を満たす必要がある。 | ||
-CompareはCompareの要件を満たす必要がある。 | ||
[編集] 複雑性
N を last - first とした場合
[編集] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[編集] 可能な実装
libstdc++ と libc++ の実装も参照してください。
[編集] 備考
LWG713 以前は、複雑性要件により `sort()` は クイックソート のみで実装可能とされており、最悪の場合で O(N2
) の比較が必要となる可能性がありました。
イントロソート は、すべてのケースを O(N·log(N)) の比較(平均ケースで追加のオーバーヘッドなし)で処理できるため、通常は `sort()` の実装に用いられます。
libc++ は、LLVM 14 まで、修正された時間複雑性要件を実装していませんでした。
[編集] 例
#include <algorithm> #include <array> #include <functional> #include <iostream> #include <string_view> int main() { std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; auto print = [&s](std::string_view const rem) { for (auto a : s) std::cout << a << ' '; std::cout << ": " << rem << '\n'; }; std::sort(s.begin(), s.end()); print("sorted with the default operator<"); std::sort(s.begin(), s.end(), std::greater<int>()); print("sorted with the standard library compare function object"); struct { bool operator()(int a, int b) const { return a < b; } } customLess; std::sort(s.begin(), s.end(), customLess); print("sorted with a custom function object"); std::sort(s.begin(), s.end(), [](int a, int b) { return a > b; }); print("sorted with a lambda expression"); }
出力
0 1 2 3 4 5 6 7 8 9 : sorted with the default operator< 9 8 7 6 5 4 3 2 1 0 : sorted with the standard library compare function object 0 1 2 3 4 5 6 7 8 9 : sorted with a custom function object 9 8 7 6 5 4 3 2 1 0 : sorted with a lambda expression
[編集] 欠陥報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 713 | C++98 | O(N·log(N)) の時間複雑度は平均の場合にのみ要求されていました。 | 最悪の場合に要求されます。 |
[編集] 関連項目
| 範囲の最初のN個の要素をソートする (関数テンプレート) | |
| 等しい要素間の順序を維持しながら要素の範囲をソートする (関数テンプレート) | |
| (C++20) |
範囲を昇順にソートする (アルゴリズム関数オブジェクト) |