名前空間
変種
操作

std::partial_sort_copy

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

順列
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template<class InputIt, class RandomIt >

RandomIt partial_sort_copy( InputIt first, InputIt last,

                            RandomIt d_first, RandomIt d_last );
(C++20未満)
template<class InputIt, class RandomIt >

constexpr RandomIt partial_sort_copy( InputIt first, InputIt last,

                                      RandomIt d_first, RandomIt d_last );
(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以上)
(3)
template<class InputIt, class RandomIt, class Compare >

RandomIt partial_sort_copy( InputIt first, InputIt last,
                            RandomIt d_first, RandomIt d_last,

                            Compare comp );
(C++20未満)
template<class InputIt, class RandomIt, class Compare >

constexpr RandomIt partial_sort_copy( InputIt first, InputIt last,
                                      RandomIt d_first, RandomIt d_last,

                                      Compare comp );
(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 はソートする要素の数 (n = min(last - first, d_last - d_first)) です。 等しい要素の順序が維持されることは保証されません。

1) 要素は operator< を用いて比較されます。
3) 要素は指定された二項比較関数 comp を用いて比較されます。
2,4)(1,3) と同じですが、 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& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはならず、 Type1 型および Type2 型 (およびそれらの const 修飾された型) のすべての値を値カテゴリに関わらず受理できなければなりません (そのため Type1 & は許されません。 また Type1 に対してムーブがコピーと同等でなければ Type1 も許されません(C++11以上))。
Type1 および Type2 は、どちらも RandomIt 型のオブジェクトの逆参照から暗黙に変換可能なものでなければなりません。 ​

型の要件
-
InputItLegacyInputIterator の要件を満たさなければなりません。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。
-
RandomItValueSwappable および 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 個の要素をソートします
(関数テンプレート)[edit]
指定範囲を昇順にソートします
(関数テンプレート)[edit]
等しい要素間の順序を維持しながら指定範囲の要素をソートします
(関数テンプレート)[edit]
close