Namespaces
Variants

std::partial_sort_copy

cppreference.net에서
 
 
알고리즘 라이브러리
제한된 알고리즘 및 범위에 대한 알고리즘 (C++20)
제한된 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 탐색 연산
(분할된 범위에 대해)
집합 연산 (정렬된 범위에 대해)
병합 연산 (정렬된 범위에 대해)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
헤더 <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) 요소는 에 대해 정렬operator<됩니다(C++20까지)std::less{}(C++20부터).
3) 요소는 comp에 대해 정렬됩니다.
2,4) (1,3)과 동일하지만 policy에 따라 실행됩니다.
이러한 오버로드는 다음 표현식의 값이 true인 경우에만 오버로드 해결에 참여합니다:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20까지)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20부터)

만약 *first이 에 쓰기 가능d_first하지 않다면, 프로그램은 잘못된 형식(ill-formed)입니다.

다음 조건 중 하나라도 만족하면 동작이 정의되지 않습니다:

  • *d_first의 유형이 Swappable이 아닙니다.
(C++11까지)
(C++11부터)

매개변수

first, last - 정렬할 요소들의 범위 를 정의하는 반복자 쌍
d_first, d_last - 정렬된 데이터가 할당될 요소들의 범위 를 정의하는 반복자 쌍
policy - 사용할 실행 정책
comp - 비교 함수 객체(즉, Compare 요구사항을 만족하는 객체)로, 첫 번째 인수가 두 번째 인수보다 작은 경우(즉, 순서상 앞서는 경우) ​ 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 이 throw됩니다.

가능한 구현

구현체는 또한 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 * first 에 쓰기가 요구되지 않았음 d_first 쓰기 가능하지 않을 경우 프로그램의 형식이 올바르지 않음

참고

범위의 처음 N개 요소를 정렬합니다
(함수 템플릿 & 알고리즘 함수 객체)
요소 범위를 정렬합니다
(함수 템플릿 & 알고리즘 함수 객체)
동등한 요소 간의 상대적 순서를 유지하면서 요소 범위를 정렬합니다
(함수 템플릿 & 알고리즘 함수 객체)
요소 범위를 복사하고 부분적으로 정렬합니다
(알고리즘 함수 객체)