std::partial_sort_copy
| ヘッダー <algorithm> で定義 |
||
| template< class InputIt, class RandomIt > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(1) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class RandomIt > |
(2) | (C++17以降) |
| template< class InputIt, class RandomIt, class Compare > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(3) | (C++20 以降 constexpr) |
| template< class ExecutionPolicy, class ForwardIt, class RandomIt, class Compare > |
(4) | (C++17以降) |
範囲 [first, last) の要素の一部を昇順にソートし、結果を範囲 [d_first, d_last) に格納します。
ソートされた要素は、範囲 [d_first, d_first + n) に最大 d_last - d_first 個まで配置されます。n はソートする要素の数です(std::min(std::distance(first, last), d_last - d_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 が d_first に書き込み可能でない場合、プログラムは不正形式です。
次のいずれかの条件が満たされる場合、動作は未定義です。
|
(C++11まで) |
|
(C++11以降) |
目次 |
[編集] パラメータ
| first, last | - | ソートする要素の 範囲 を定義するイテレータのペア |
| d_first, d_last | - | ソートされたデータが代入される範囲を定義するイテレータのペア |
| policy | - | 使用する 実行ポリシー |
| comp | - | 比較関数オブジェクト(つまり、Compare の要件を満たすオブジェクト)。最初の引数が2番目の引数より小さい(つまり、前に順序付けられる)場合に true を返します。 比較関数のシグネチャは、以下と同等でなければならない。 bool cmp(const Type1& a, const Type2& b); シグネチャは const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、値カテゴリ に関係なく、(おそらく const の) |
| 型要件 | ||
-InputIt は LegacyInputIterator の要件を満たす必要があります。 | ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。 | ||
-RandomItはLegacyRandomAccessIteratorの要件を満たす必要がある。 | ||
-CompareはCompareの要件を満たす必要がある。 | ||
[編集] 戻り値
ソートされた範囲の上限を定義する要素へのイテレータ。すなわち d_first + std::min(std::distance(first, last), d_last - d_first)。
[編集] 計算量
N を std::distance(first, last)、D を d_last - d_first とすると
[編集] 例外
ExecutionPolicy というテンプレートパラメータを持つオーバーロードは、次のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外をスローし、
ExecutionPolicyが 標準ポリシー のいずれかである場合、std::terminate が呼び出されます。その他のExecutionPolicyの場合、動作は実装定義です。 - アルゴリズムがメモリの割り当てに失敗した場合、std::bad_alloc がスローされます。
[編集] 実装例
libstdc++ および libc++ の実装を参照してください。
[編集] 例
以下のコードは、整数のベクターをソートし、それをより小さいベクターとより大きいベクターにコピーします。
#include <algorithm> #include <functional> #include <iostream> #include <string_view> #include <type_traits> #include <vector> void println(std::string_view rem, const auto& v) { std::cout << rem; if constexpr (std::is_scalar_v<std::decay_t<decltype(v)>>) std::cout << v; else for (int e : v) std::cout << e << ' '; std::cout << '\n'; } int main() { const auto v0 = {4, 2, 5, 1, 3}; std::vector<int> v1{10, 11, 12}; std::vector<int> v2{10, 11, 12, 13, 14, 15, 16}; std::vector<int>::iterator it; it = std::partial_sort_copy(v0.begin(), v0.end(), v1.begin(), v1.end()); println("Writing to the smaller vector in ascending order gives: ", v1); if (it == v1.end()) println("The return value is the end iterator", ' '); it = std::partial_sort_copy(v0.begin(), v0.end(), v2.begin(), v2.end(), std::greater<int>()); println("Writing to the larger vector in descending order gives: ", v2); println("The return value is the iterator to ", *it); }
出力
Writing to the smaller vector in ascending order gives: 1 2 3 The return value is the end iterator Writing to the larger vector in descending order gives: 5 4 3 2 1 15 16 The return value is the iterator to 15
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| P0896R4 | C++98 | *first は d_first に書き込み可能である必要はありませんでした。 | 書き込み可能でない場合、プログラムは不正形式となります。 |
[編集] 関連項目
| 範囲の最初のN個の要素をソートする (関数テンプレート) | |
| 範囲を昇順にソートする (関数テンプレート) | |
| 等しい要素間の順序を維持しながら要素の範囲をソートする (関数テンプレート) | |
| (C++20) |
要素の範囲をコピーして部分的にソートする (アルゴリズム関数オブジェクト) |