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 내 요소들의 순서는 지정되지 않습니다.
참조나 반복자가 무효화되지 않습니다.
목차매개변수
복잡도주어진 N 이 std:: distance ( begin ( ) , end ( ) ) 인 경우:
1)
약
N·log(N)
번의 비교를
operator
<
를 사용하여 수행합니다.
2)
대략
N·log(N)
번의 비교 함수
comp
적용.
참고 사항
std::sort
는 임의 접근 반복자를 요구하므로
예제이 코드 실행 #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
참고 항목
| |||||||||||