std::forward_list<T,Allocator>:: sort
|
void
sort
(
)
;
|
(1) |
(C++11부터)
(C++26부터 constexpr) |
|
template
<
class
Compare
>
void sort ( Compare comp ) ; |
(2) |
(C++11부터)
(C++26부터 constexpr) |
요소를 정렬하고 동등한 요소들의 순서를 유지합니다. 예외가 발생하면 * this 내 요소들의 순서는 지정되지 않습니다.
참조나 반복자가 무효화되지 않습니다.
목차 |
매개변수
| comp | - |
비교 함수 객체(즉,
Compare
요구 사항을 만족하는 객체)로, 첫 번째 인수가 두 번째 인수보다
작은
경우(즉, 순서상
앞서는
경우)
true
를 반환합니다.
비교 함수의 시그니처는 다음에 부합해야 합니다: bool cmp ( const Type1 & a, const Type2 & b ) ;
시그니처가 반드시
const
&
를 가질 필요는 없지만, 함수는 전달된 객체를 수정해서는 안 되며
값 범주
와 무관하게 (가능하다면 const)
|
| 타입 요구 사항 | ||
-
Compare
는
Compare
요구 사항을 충족해야 합니다.
|
||
복잡도
주어진 N 이 std:: distance ( begin ( ) , end ( ) ) 인 경우:
참고 사항
std::sort
는 임의 접근 반복자를 요구하므로
forward_list
와 함께 사용할 수 없습니다. 이 함수는 또한
std::sort
와 달리
forward_list
의 요소 타입이 교환 가능할 필요가 없으며, 모든 반복자의 값을 보존하고 안정 정렬을 수행합니다.
예제
#include <functional> #include <iostream> #include <forward_list> std::ostream& operator<<(std::ostream& ostr, const std::forward_list<int>& list) { for (const int i : list) ostr << ' ' << i; return ostr; } int main() { std::forward_list<int> list{8, 7, 5, 9, 0, 1, 3, 2, 6, 4}; std::cout << "initially: " << list << '\n'; list.sort(); std::cout << "ascending: " << list << '\n'; list.sort(std::greater<int>()); std::cout << "descending:" << list << '\n'; }
출력:
initially: 8 7 5 9 0 1 3 2 6 4 ascending: 0 1 2 3 4 5 6 7 8 9 descending: 9 8 7 6 5 4 3 2 1 0
참고 항목
|
요소들의 순서를 반전시킵니다
(public member function) |