GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::ranges::partial_sort_copy - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. algorithm
  3. partial_sort_copy

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<algorithm>

std::ranges::partial_sort_copy(C++20)

namespace std::ranges {
  template <input_iterator I1,
            sentinel_for<I1> S1,
            random_access_iterator I2, sentinel_for<I2> S2,
            class Comp = ranges::less,
            class Proj1 = identity,
            class Proj2 = identity>
    requires indirectly_copyable<I1, I2> &&
             sortable<I2, Comp, Proj2> &&
             indirect_strict_weak_order<Comp, projected<I1, Proj1>, 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 <input_range R1,
            random_access_range R2,
            class Comp = ranges::less,
            class Proj1 = identity,
            class Proj2 = identity>
    requires indirectly_copyable<iterator_t<R1>, iterator_t<R2>> &&
             sortable<iterator_t<R2>, Comp, Proj2> &&
             indirect_strict_weak_order<
               Comp,
               projected<iterator_t<R1>, Proj1>,
               projected<iterator_t<R2>, Proj2>
             >
  constexpr partial_sort_copy_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
    partial_sort_copy(R1&& r,
                      R2&& result_r,
                      Comp comp = {},
                      Proj1 proj1 = {},
                      Proj2 proj2 = {}); // (2) C++20

  template <execution-policy Ep,
            random_access_iterator I1,
            sized_sentinel_for<I1> S1,
            random_access_iterator I2,
            sized_sentinel_for<I2> S2,
            class Comp = ranges::less,
            class Proj1 = identity,
            class Proj2 = identity>
    requires indirectly_copyable<I1, I2> &&
             sortable<I2, Comp, Proj2> &&
             indirect_strict_weak_order<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
  partial_sort_copy_result<I1, I2>
    partial_sort_copy(Ep&& exec,
                      I1 first,
                      S1 last,
                      I2 result_first,
                      S2 result_last,
                      Comp comp = {},
                      Proj1 proj1 = {},
                      Proj2 proj2 = {}); // (3) C++26

  template <execution-policy Ep,
            sized-random-access-range R1,
            sized-random-access-range R2,
            class Comp = ranges::less,
            class Proj1 = identity,
            class Proj2 = identity>
    requires indirectly_copyable<iterator_t<R1>, iterator_t<R2>> &&
             sortable<iterator_t<R2>, Comp, Proj2> &&
             indirect_strict_weak_order<
               Comp,
               projected<iterator_t<R1>, Proj1>,
               projected<iterator_t<R2>, Proj2>
             >
  partial_sort_copy_result<borrowed_iterator_t<R1>, borrowed_iterator_t<R2>>
    partial_sort_copy(Ep&& exec,
                      R1&& r,
                      R2&& result_r,
                      Comp comp = {},
                      Proj1 proj1 = {},
                      Proj2 proj2 = {}); // (4) C++26
}

概要

範囲を部分的にソートした結果を他の範囲にコピーする

  • (1): イテレータ範囲を指定する
  • (2): Rangeを直接指定する
  • (3): (1)の並列アルゴリズム版。実行ポリシーを指定する
  • (4): (2)の並列アルゴリズム版。実行ポリシーを指定する

効果

N = min(last - first, result_last - result_first) とする。

[first,last) にある要素の中から、N 個の要素をソート済みの状態で [result_first,result_first + N) に配置する。

戻り値

partial_sort_copy_result {
  .in = last,
  .out = result_first + N
}

計算量

ほぼ (last - first) * log(N) 回の比較を行う

例

基本的な使い方

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<int> v = {3, 1, 4, 2, 5};

  // vから小さい順に2要素取り出す
  std::vector<int> result(2);
  std::ranges::partial_sort_copy(v, result);

  for (int i : result) {
    std::cout << i;
  }
  std::cout << std::endl;
}

出力

12

並列アルゴリズムの例 (C++26)

#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> src = {3, 1, 4, 1, 5, 9, 2, 6};
  std::vector<int> dst(3);

  // 並列に上位3個の要素をソートしてコピー
  std::ranges::partial_sort_copy(std::execution::par,
                                 src, dst);

  for (int x : dst) {
    std::cout << x << ' ';
  }
  std::cout << std::endl;
}

出力

1 1 2

バージョン

言語

  • C++20

処理系

  • Clang: ??
  • GCC: 10.1.0 ✅
  • Visual C++: 2019 Update 10 ✅

参照

  • N4861 25 Algorithms library
  • P3179R9 C++ parallel range algorithms
  • LWG Issue 3186. Ranges remove, partition, and partial_sort_copy algorithms discard useful information
    • C++20で、これらのアルゴリズムが処理後の終端イテレータ等の有用な情報を返すよう、戻り値がsubrangeや結果型に変更された
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.