Namespaces
Variants

std:: partial_sort_copy

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定義先ヘッダ <algorithm>
template < class InputIt, class RandomIt >

RandomIt partial_sort_copy ( InputIt first, InputIt last,

RandomIt d_first, RandomIt d_last ) ;
(1) (C++20以降 constexpr)
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) (C++20以降 constexpr)
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) 要素は sorted されており、 operator < (until C++20) std:: less { } (since C++20) に従ってソートされています。
3) 要素は comp に関してソートされます。
2,4) (1,3) と同様ですが、 policy に従って実行されます。
これらのオーバーロードは、以下の全ての条件が満たされる場合にのみオーバーロード解決に参加します:

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 に対して writable でない場合、プログラムは不適格(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個の要素をソートする
(関数テンプレート)
範囲を昇順にソートする
(関数テンプレート)
等しい要素間の順序を維持しながら範囲の要素をソートする
(関数テンプレート)
範囲の要素をコピーして部分的にソートする
(アルゴリズム関数オブジェクト)