std::partial_sort_copy
| ヘッダ <algorithm>
|
||
template< class InputIt, class RandomIt >
RandomIt partial_sort_copy( InputIt first, InputIt last,
RandomIt d_first, RandomIt d_last );
|
で定義 (1) | (constexpr C++20以降) |
template< class ExecutionPolicy,
class ForwardIt, class RandomIt >
RandomIt partial_sort_copy( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
RandomIt d_first, RandomIt d_last );
|
(2) | (C++17以降) |
template< class InputIt, class RandomIt, class Compare >
RandomIt partial_sort_copy( InputIt first, InputIt last,
RandomIt d_first, RandomIt d_last,
Compare comp );
|
(3) | (constexpr C++20以降) |
template< class ExecutionPolicy,
class ForwardIt, class RandomIt, class Compare >
RandomIt partial_sort_copy( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
RandomIt d_first, RandomIt d_last,
Compare comp );
|
(4) | (C++17以降) |
範囲 [first, last) 内の一部の要素を昇順にソートし、その結果を範囲 [d_first, d_last) に格納します。
多くとも d_last - d_first 個の要素がソートされて範囲 [d_first, d_first + n) に配置されます。 n はソートする要素の数です (std::min(std::distance(first, last), d_last - d_first))。等しい要素の順序は保持されるとは限りません。
comp に関してソートされます。policy に従って実行されます。true である場合にのみオーバーロード解決に参加します:
|
|
(C++20まで) |
|
|
(C++20以降) |
もし *first が writable でないならば d_first、プログラムは ill-formed です。
次の条件のいずれかが満たされる場合、動作は未定義です:
|
(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 | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P0896R4 | C++98 | * first への書き込みは必須ではなかった | 書き込み可能でない場合、プログラムは不適格となる |
関連項目
| 範囲の最初のN個の要素をソートする (関数テンプレート&アルゴリズム関数オブジェクト) | |
(C++20) |
|
| 範囲の要素をソートする (関数テンプレート&アルゴリズム関数オブジェクト) | |
(C++20) |
|
| 等価な要素間の相対順序を維持しながら範囲の要素をソートする (関数テンプレート&アルゴリズム関数オブジェクト) | |
(C++20) |
|
(C++20) |
範囲の要素をコピーして部分的にソートする (アルゴリズム関数オブジェクト) |