std::partial_sort_copy
ヘッダ <algorithm> で定義
|
||
(1) | ||
template< class InputIt, class RandomIt > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(C++20未満) | |
template< class InputIt, class RandomIt > constexpr RandomIt partial_sort_copy( InputIt first, InputIt last, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class RandomIt > RandomIt partial_sort_copy( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, |
(2) | (C++17以上) |
(3) | ||
template< class InputIt, class RandomIt, class Compare > RandomIt partial_sort_copy( InputIt first, InputIt last, |
(C++20未満) | |
template< class InputIt, class RandomIt, class Compare > constexpr RandomIt partial_sort_copy( InputIt first, InputIt last, |
(C++20以上) | |
template< class ExecutionPolicy, class ForwardIt, class RandomIt, class Compare > RandomIt partial_sort_copy( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, |
(4) | (C++17以上) |
範囲 [first, last)
内の要素の一部を昇順にソートし、結果を範囲 [d_first, d_last)
に格納します。
多くとも d_last - d_first 個の要素が範囲 [d_first, d_first + n)
にソートされて格納されます。 n
はソートする要素の数 (n = min(last - first, d_last - d_first)) です。 等しい要素の順序が維持されることは保証されません。
operator<
を用いて比較されます。comp
を用いて比較されます。policy
に従って実行されます。 このオーバーロードは、std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> が true である場合にのみ、オーバーロード解決に参加します。目次 |
[編集] 引数
first, last | - | ソートする要素の範囲 |
d_first, d_last | - | コピー先の範囲を定義するランダムアクセスイテレータ |
policy | - | 使用する実行ポリシー。 詳細は実行ポリシーを参照してください |
comp | - | 最初の要素が2番目の要素より小さい (前に順序づけられる) 場合に true を返す、比較関数オブジェクト (Compare の要件を満たすオブジェクト)。 比較関数のシグネチャは以下と同等なものであるべきです。 bool cmp(const Type1 &a, const Type2 &b); シグネチャが const & を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、 |
型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-ForwardIt は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-RandomIt は ValueSwappable および LegacyRandomAccessIterator の要件を満たさなければなりません。
| ||
-RandomIt を逆参照した型は MoveAssignable および MoveConstructible の要件を満たさなければなりません。
|
[編集] 戻り値
ソート済み範囲の上限を定義する要素を指すイテレータ、すなわち d_first + min(last - first, d_last - d_first)。
[編集] 計算量
O(N·log(min(D,N)) 回の cmp
の適用、ただし N = std::distance(first, last), D = std::distance(d_first, d_last) です。
[編集] 例外
テンプレート引数 ExecutionPolicy
を持つオーバーロードは以下のようにエラーを報告します。
- アルゴリズムの一部として呼び出された関数の実行が例外を投げ、
ExecutionPolicy
が標準のポリシーのいずれかの場合は、 std::terminate が呼ばれます。 それ以外のあらゆるExecutionPolicy
については、動作は処理系定義です。 - アルゴリズムがメモリの確保に失敗した場合は、 std::bad_alloc が投げられます。
[編集] 例
以下のコードは整数のベクタをソートし、それをより小さいおよびより大きいベクタにコピーします。
#include <algorithm> #include <vector> #include <functional> #include <iostream> int main() { std::vector<int> 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()); std::cout << "Writing to the smaller vector in ascending order gives: "; for (int a : v1) { std::cout << a << " "; } std::cout << '\n'; if(it == v1.end()) std::cout << "The return value is the end iterator\n"; it = std::partial_sort_copy(v0.begin(), v0.end(), v2.begin(), v2.end(), std::greater<int>()); std::cout << "Writing to the larger vector in descending order gives: "; for (int a : v2) { std::cout << a << " "; } std::cout << '\n' << "The return value is the iterator to " << *it << '\n'; }
出力:
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
[編集] 関連項目
指定範囲の最初の N 個の要素をソートします (関数テンプレート) | |
指定範囲を昇順にソートします (関数テンプレート) | |
等しい要素間の順序を維持しながら指定範囲の要素をソートします (関数テンプレート) |