ranges::partial_sort_copy

ranges::partial_sort_copy (부분 정렬 복사 — ranges)

입력 범위를 정렬해 앞의 일부를 출력 버퍼에 복사하는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::partial_sort_copy[first, last)의 요소 중 가장 작은 N개(N은 출력 크기)를 정렬된 순서로 [result_first, result_last)에 복사해요.

namespace std::ranges {
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>
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 = {} );
}
  • 반환 타입 partial_sort_copy_result{in, out}.
  • 입력은 정렬하지 않고 결과를 별도 버퍼에 담아요.
std::vector<int> src{9, 2, 7, 1, 5};
std::vector<int> out(3);
std::ranges::partial_sort_copy(src, out.begin(), out.end());
// out == {1,2,5}

원본을 보존하며 "정렬된 상위 k개"를 뽑는 ranges 버전이에요.

더 알아보기 (Learn more)

cppreference