About Sort () in std :: List

xiaoxiao2021-03-06  39

About Sort () in std :: List

List :: sort

Void Sort (); Template Void Sort (PRED PR);

These two member functions are sorted in the predicates described below. It is assumed that the iterator PI points to the elements located at position I, PJ points to the elements at position J, when i

#include strunt; int Nend; Bool Operator <(mystruct item) / * Note overload <* / {if (nStart

Std :: List mylist; std :: list mystructList; INT i; for (i = 9; i> -1; i -) {mylist.push_back (i);

For (i = 9; i> -1; i -) {mystruct.nstart = i; mystruct.nend = 9-i; mystructlist.push_back (mystruct);}

Std :: List :: Iterator Nitem; Printf ("/ NMYLIST Sort: / N"); for (NItem = MyList.Begin (); NITEM! = mylist.end (); NITEM ) {Printf (" % D / T ", (* nitem));

MYList.Sort (); Printf ("/ NMYLIST: / N"); for (NItem = mylist.begin (); NITEM! = mylist.end (); NITEM ) {Printf ("% D / T", (* nitem));

Std :: List :: item; "/ nmystructList before: / n"); for (item = mystructlist.begin (); item! = mystructlist.end (); item ) {printf (" NStart =% D / T ", (* iTEM) .nstart);} mystructList.sort (); printf (" / nmystructList After sorting: / n "); for (item = mystructlist.begin (); item ! = mYStructList.end (); item ) {Printf ("nStart =% D / T", (* item) .nstart);} printf ("/ n");

Return 0;} The result is: sort

MYLIST Sort: 9 8 7 6 5 4 3 2 1 0

MYLIST Sort by: 0 1 2 3 4 5 6 7 8 9

MyStructList before sort: nStart = 9 nStart = 8 nStart = 7 nStart = 6 nStart = 5nStart = 4 nStart = 3 nStart = 2 nStart = 1 nStart = 0

MyStructList is sorted by member NStart: nStart = 0 nStart = 1 nStart = 2 nStart = 3 nStart = 4NStart = 5 nStart = 6 nStart = 7 nStart = 8 nStart = 9

转载请注明原文地址:https://www.9cbs.com/read-71023.html

New Post(0)