Namespaces
Variants

std::partial_sort_copy

ja.cppreference.net より
 
 
アルゴリズムライブラリ
制約付きアルゴリズムと範囲に対するアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
ソートと関連操作
分割操作
(C++11)    

ソート操作
二分探索操作
(分割済み範囲に対する)
集合操作 (ソート済み範囲に対する)
マージ操作 (ソート済み範囲に対する)
ヒープ操作
最小値/最大値操作
(C++11)
(C++17)
辞書順比較操作
置換操作


 
ヘッダ <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))。等しい要素の順序は保持されるとは限りません。

1) 要素は に関してソートoperator<されます (C++20まで)std::less{}(C++20以降)。
3) 要素は comp に関してソートされます。
2,4) (1,3) と同じですが、policy に従って実行されます。
これらのオーバーロードは、次の式の値が true である場合にのみオーバーロード解決に参加します:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20まで)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(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) Type1 と Type2 のすべての値を受け入れられなければならない(したがって、 Type1& は許可されない 、 Type1 も Type1 に対してムーブがコピーと等価でない限り許可されない (C++11以降) )。
型 Type1 と Type2 は、 RandomIt 型のオブジェクトが間接参照され、それら両方に暗黙的に変換可能でなければならない。 ​

型要件
-
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 として:

1,2) 約 N·log(min(N,D)) 回の比較を operator < (C++20まで) std:: less { } (C++20以降) を使用して行います。
3,4) 約 N·log(min(N,D)) 回の比較子 comp の適用。

例外

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個の要素をソートする
(関数テンプレート&アルゴリズム関数オブジェクト)
範囲の要素をソートする
(関数テンプレート&アルゴリズム関数オブジェクト)
等価な要素間の相対順序を維持しながら範囲の要素をソートする
(関数テンプレート&アルゴリズム関数オブジェクト)
範囲の要素をコピーして部分的にソートする
(アルゴリズム関数オブジェクト)