std::forward_list<T,Allocator>:: sort
|
void
sort
(
)
;
|
(1) |
(C++11以降)
(constexprはC++26以降) |
|
template
<
class
Compare
>
void sort ( Compare comp ) ; |
(2) |
(C++11以降)
(constexprはC++26以降) |
要素をソートし、同等な要素の順序を保持します。例外がスローされた場合、 * 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
関連項目
| |||||||||||