Namespaces
Variants

std::ranges:: partial_sort_copy, std::ranges:: partial_sort_copy_result

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
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
(注:このHTML要素には翻訳対象となるテキストコンテンツが含まれていません。すべてのタグと属性は原文のまま保持されています。)
定義済みヘッダー <algorithm>
呼び出しシグネチャ
template < std:: input_iterator I1, std:: sentinel_for < I1 > S1,

std:: random_access_iterator I2, std:: sentinel_for < I2 > S2,
class Comp = ranges:: less , class Proj1 = std:: identity ,
class Proj2 = std:: identity >
requires std:: indirectly_copyable < I1, I2 > &&
std:: sortable < I2, Comp, Proj2 > &&
std:: indirect_strict_weak_order < Comp, std :: projected < I1, Proj1 > ,
std :: projected < I2, Proj2 >>
constexpr partial_sort_copy_result < I1, I2 >
partial_sort_copy ( I1 first, S1 last, I2 result_first, S2 result_last,

Comp comp = { } , Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(1) (C++20以降)
template < ranges:: input_range R1, ranges:: random_access_range R2,

class Comp = ranges:: less , class Proj1 = std:: identity ,
class Proj2 = std:: identity >
requires std:: indirectly_copyable < ranges:: iterator_t < R1 > , ranges:: iterator_t < R2 >> &&
std:: sortable < ranges:: iterator_t < R2 > , Comp, Proj2 > &&
std:: indirect_strict_weak_order < Comp, std :: projected < ranges:: iterator_t < R1 > ,
Proj1 > , std :: projected < ranges:: iterator_t < R2 > , Proj2 >>
constexpr partial_sort_copy_result < ranges:: borrowed_iterator_t < R1 > ,
ranges:: borrowed_iterator_t < R2 >>
partial_sort_copy ( R1 && r, R2 && result_r,

Comp comp = { } , Proj1 proj1 = { } , Proj2 proj2 = { } ) ;
(2) (C++20以降)
ヘルパー型
template < class I, class O >
using partial_sort_copy_result = ranges:: in_out_result < I, O > ;
(3) (C++20以降)

ソース範囲 [ first , last ) から最初の N 要素を、あたかも comp proj1 に対して部分的にソートされたかのように、宛先範囲 [ result_first , result_first + N ) にコピーします。ここで N = min(L₁, L₂) L₁ ranges:: distance ( first, last ) に等しく、 L₂ ranges:: distance ( result_first, result_last ) に等しい値です。

等しい要素の順序は 保持される 保証はありません。

1) ソース範囲の要素は関数オブジェクト proj1 を使用して投影され、デスティネーションの要素は関数オブジェクト proj2 を使用して投影されます。
2) (1) と同様だが、 r をソース範囲として、 result_r を宛先範囲として使用する。すなわち、 ranges:: begin ( r ) first として、 ranges:: end ( r ) last として、 ranges:: begin ( result_r ) result_first として、 ranges:: end ( result_r ) result_last として使用する場合と同等である。

このページで説明されている関数ライクなエンティティは、 アルゴリズム関数オブジェクト (非公式には niebloids として知られる)です。すなわち:

目次

パラメータ

first, last - コピー元の要素を定義するイテレータ-センチネルペア range
r - コピー元のソース範囲
result_first, result_last - 要素のコピー先を定義するイテレータ-センチネルペア range
result_r - コピー先の範囲
comp - 投影された要素に適用する比較関数
proj1 - ソース範囲の要素に適用する投影関数
proj2 - コピー先範囲の要素に適用する投影関数

戻り値

{ last, result_first + N } に等しいオブジェクト。

計算量

最大で L₁•log(N) 回の比較と 2•L₁•log(N) 回の射影を行います。

実装例

struct partial_sort_copy_fn
{
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::random_access_iterator I2, std::sentinel_for<I2> S2,
             class Comp = ranges::less, class Proj1 = std::identity,
             class Proj2 = std::identity>
    requires std::indirectly_copyable<I1, I2> && std::sortable<I2, Comp, Proj2> &&
             std::indirect_strict_weak_order<Comp, std::projected<I1, Proj1>,
             std::projected<I2, Proj2>>
    constexpr ranges::partial_sort_copy_result<I1, I2>
        operator()(I1 first, S1 last, I2 result_first, S2 result_last,
                   Comp comp = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        if (result_first == result_last)
            return {std::move(ranges::next(std::move(first), std::move(last))),
                    std::move(result_first)};
        auto out_last{result_first};
        // 最初のN個の要素をコピー
        for (; !(first == last or out_last == result_last); ++out_last, ++first)
            *out_last = *first;
        // N個のコピーされた要素を最大ヒープに変換する
        ranges::make_heap(result_first, out_last, comp, proj2);
        // 入力範囲の残り(もしあれば)を処理し、ヒープ特性を保持する
        for (; first != last; ++first)
        {
            if (std::invoke(comp, std::invoke(proj1, *first),
                                  std::invoke(proj2, *result_first)))
            {
                // 最大の項目を取り出し、新しく見つかったより小さい項目を挿入する
                ranges::pop_heap(result_first, out_last, comp, proj2);
                *(out_last - 1) = *first;
                ranges::push_heap(result_first, out_last, comp, proj2);
            }
        }
        // 出力範囲の最初のN要素は依然として
        // ヒープ - ソート済み範囲に変換する
        ranges::sort_heap(result_first, out_last, comp, proj2);
        return {std::move(first), std::move(out_last)};
    }
    template<ranges::input_range R1, ranges::random_access_range R2,
             class Comp = ranges::less, class Proj1 = std::identity,
             class Proj2 = std::identity>
    requires std::indirectly_copyable<ranges::iterator_t<R1>, ranges::iterator_t<R2>> &&
             std::sortable<ranges::iterator_t<R2>, Comp, Proj2> &&
             std::indirect_strict_weak_order<Comp, std::projected<ranges::iterator_t<R1>,
             Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>>
    constexpr ranges::partial_sort_copy_result<ranges::borrowed_iterator_t<R1>,
              ranges::borrowed_iterator_t<R2>>
        operator()(R1&& r, R2&& result_r, Comp comp = {},
                   Proj1 proj1 = {}, Proj2 proj2 = {}) const
    {
        return (*this)(ranges::begin(r), ranges::end(r),
                       ranges::begin(result_r), ranges::end(result_r),
                       std::move(comp), std::move(proj1), std::move(proj2));
    }
};
inline constexpr partial_sort_copy_fn partial_sort_copy {};

#include <algorithm>
#include <forward_list>
#include <functional>
#include <iostream>
#include <ranges>
#include <string_view>
#include <vector>
void print(std::string_view rem, std::ranges::input_range auto const& v)
{
    for (std::cout << rem; const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    const std::forward_list source{4, 2, 5, 1, 3};
    print("Write to the smaller vector in ascending order: ", "");
    std::vector dest1{10, 11, 12};
    print("const source list: ", source);
    print("destination range: ", dest1);
    std::ranges::partial_sort_copy(source, dest1);
    print("partial_sort_copy: ", dest1);
    print("Write to the larger vector in descending order:", "");
    std::vector dest2{10, 11, 12, 13, 14, 15, 16};
    print("const source list: ", source);
    print("destination range: ", dest2);
    std::ranges::partial_sort_copy(source, dest2, std::greater{});
    print("partial_sort_copy: ", dest2);
}

出力:

Write to the smaller vector in ascending order:
const source list: 4 2 5 1 3
destination range: 10 11 12
partial_sort_copy: 1 2 3
Write to the larger vector in descending order:
const source list: 4 2 5 1 3
destination range: 10 11 12 13 14 15 16
partial_sort_copy: 5 4 3 2 1 15 16

関連項目

範囲の最初のN個の要素をソートする
(アルゴリズム関数オブジェクト)
範囲を昇順にソートする
(アルゴリズム関数オブジェクト)
等しい要素間の順序を維持しながら範囲の要素をソートする
(アルゴリズム関数オブジェクト)
最大ヒープを昇順にソートされた要素の範囲に変換する
(アルゴリズム関数オブジェクト)
要素の範囲から最大ヒープを作成する
(アルゴリズム関数オブジェクト)
最大ヒープに要素を追加する
(アルゴリズム関数オブジェクト)
最大ヒープから最大要素を削除する
(アルゴリズム関数オブジェクト)
要素の範囲をコピーして部分的にソートする
(関数テンプレート)