名前空間
変種
操作

std::partial_sort_copy

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
partial_sort_copy
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <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以降)

範囲 [firstlast) の要素の一部を昇順にソートし、結果を範囲 [d_firstd_last) に格納します。

ソートされた要素は、範囲 [d_firstd_first + n) に最大 d_last - d_first 個まで配置されます。n はソートする要素の数です(std::min(std::distance(first, last), d_last - d_first))。等しい要素の順序は保持される保証はありません。

1) 要素は sorted で、operator<(C++20まで)std::less{}(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以降)

もし *firstd_first書き込み可能でない場合、プログラムは不正形式です。

次のいずれかの条件が満たされる場合、動作は未定義です。

  • *d_first の型はSwappableではありません。
(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 の) Type1Type2 のすべての値を受け入れられる必要があります (したがって、Type1& は許可されません。また、Type1 のムーブがコピーと同等でない限り、Type1 も許可されません(C++11以降))。
Type1Type2 の型は、RandomIt 型のオブジェクトを間接参照し、その後それらの両方に暗黙的に変換できるような型でなければなりません。​

型要件
-
InputItLegacyInputIterator の要件を満たす必要があります。
-
ForwardItLegacyForwardIterator の要件を満たさなければなりません。
-
RandomItLegacyRandomAccessIteratorの要件を満たす必要がある。
-
CompareCompareの要件を満たす必要がある。

[編集] 戻り値

ソートされた範囲の上限を定義する要素へのイテレータ。すなわち d_first + std::min(std::distance(first, last), d_last - d_first)

[編集] 計算量

Nstd::distance(first, last)Dd_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 適用対象 公開された動作 正しい動作
P0896R4 C++98 *firstd_first に書き込み可能である必要はありませんでした。 書き込み可能でない場合、プログラムは不正形式となります。

[編集] 関連項目

範囲の最初のN個の要素をソートする
(関数テンプレート) [編集]
範囲を昇順にソートする
(関数テンプレート) [編集]
等しい要素間の順序を維持しながら要素の範囲をソートする
(関数テンプレート) [編集]
要素の範囲をコピーして部分的にソートする
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)