std:: sort
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
RandomIt
>
void sort ( RandomIt first, RandomIt last ) ; |
(1) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
RandomIt
>
void
sort
(
ExecutionPolicy
&&
policy,
|
(2) | (C++17부터) |
|
template
<
class
RandomIt,
class
Compare
>
void sort ( RandomIt first, RandomIt last, Compare comp ) ; |
(3) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
RandomIt,
class
Compare
>
void
sort
(
ExecutionPolicy
&&
policy,
|
(4) | (C++17부터) |
범위
[
first
,
last
)
내의 요소들을 비내림차순으로 정렬합니다. 동일한 요소들의 순서는 유지된다는 보장이 없습니다.
|
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 이후) |
다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:
|
(C++11 이전) |
|
(C++11 이후) |
목차 |
매개변수
| first, last | - | 정렬할 요소들의 범위 를 정의하는 반복자 쌍 |
| policy | - | 사용할 실행 정책 |
| comp | - |
비교 함수 객체(즉
Compare
요구사항을 만족하는 객체)로 첫 번째 인수가 두 번째 인수보다
작은 경우
(즉
먼저 정렬되는 경우
)
true
를 반환함
비교 함수의 시그니처는 다음과 동일해야 함: bool cmp ( const Type1 & a, const Type2 & b ) ;
시그니처에
const
&
가 필요하지는 않지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
에 관계없이
|
| 타입 요구사항 | ||
-
RandomIt
는
LegacyRandomAccessIterator
요구사항을 충족해야 함
|
||
-
Compare
는
Compare
요구사항을 충족해야 함
|
||
복잡도
주어진 N 을 last - first 로:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
구현체는 또한 libstdc++ 와 libc++ 에서 확인할 수 있습니다.
참고 사항
LWG713 이전에는,
LWG713
복잡도 요구사항이
sort()
구현에
Quicksort
만을 사용하는 것을 허용했으며, 이는 최악의 경우
O(N
2
)
비교 연산이 필요할 수 있습니다.
인트로소트
는 모든 경우에 대해
O(N·log(N))
비교 연산으로 처리할 수 있으며(평균적인 경우 추가 오버헤드 없이), 따라서 일반적으로
sort()
구현에 사용됩니다.
libc++는 수정된 시간 복잡도 요구사항을 LLVM 14까지 구현하지 않았습니다.
예제
#include <algorithm> #include <array> #include <functional> #include <iostream> #include <string_view> int main() { std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; auto print = [&s](std::string_view const rem) { for (auto a : s) std::cout << a << ' '; std::cout << ": " << rem << '\n'; }; std::sort(s.begin(), s.end()); print("sorted with the default operator<"); std::sort(s.begin(), s.end(), std::greater<int>()); print("sorted with the standard library compare function object"); struct { bool operator()(int a, int b) const { return a < b; } } customLess; std::sort(s.begin(), s.end(), customLess); print("sorted with a custom function object"); std::sort(s.begin(), s.end(), [](int a, int b) { return a > b; }); print("sorted with a lambda expression"); }
출력:
0 1 2 3 4 5 6 7 8 9 : sorted with the default operator< 9 8 7 6 5 4 3 2 1 0 : sorted with the standard library compare function object 0 1 2 3 4 5 6 7 8 9 : sorted with a custom function object 9 8 7 6 5 4 3 2 1 0 : sorted with a lambda expression
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 713 | C++98 | O(N·log(N)) 시간 복잡도가 평균에 대해서만 요구됨 | 최악의 경우에도 요구됨 |
참고 항목
|
범위의 첫 N개 요소를 정렬합니다
(function template) |
|
|
동일한 요소들 사이의 순서를 유지하며 범위를 정렬합니다
(function template) |
|
|
(C++20)
|
범위를 오름차순으로 정렬합니다
(algorithm function object) |