std::forward_list<T,Allocator>:: sort
|
void
sort
(
)
;
|
(1) |
(seit C++11)
(constexpr seit C++26) |
|
template
<
class
Compare
>
void sort ( Compare comp ) ; |
(2) |
(seit C++11)
(constexpr seit C++26) |
Sortiert die Elemente und bewahrt die Reihenfolge äquivalenter Elemente. Wenn eine Exception ausgelöst wird, ist die Reihenfolge der Elemente in * this nicht spezifiziert.
Keine Referenzen oder Iteratoren werden ungültig.
InhaltParameter
KomplexitätGegeben N als std:: distance ( begin ( ) , end ( ) ) :
1)
Ungefähr
N·log(N)
Vergleiche unter Verwendung von
operator
<
.
2)
Ungefähr
N·log(N)
Anwendungen der Vergleichsfunktion
comp
.
Hinweise
std::sort
erfordert Random-Access-Iteratoren und kann daher nicht mit
BeispielDiesen Code ausführen #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';
}
Ausgabe: 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
Siehe auch
| |||||||||||