Prerequisiti: std::sort in C++ , vettore in C++ , inizializzare un vettore in C++ .
CPP
tostring java
// C++ program to sort a vector in non-decreasing> // order.> #include> using> namespace> std;> > int> main()> {> > vector<> int> >v{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };> > > sort(v.begin(), v.end());> > > cout <<> 'Sorted
'> ;> > for> (> auto> x : v)> > cout << x <<> ' '> ;> > > return> 0;> }> |
>
>Produzione
Sorted 0 1 2 3 4 5 6 7 8 9>
Come ordinare in ordine decrescente?
sort() accetta un terzo parametro utilizzato per specificare l'ordine in cui devono essere ordinati gli elementi. Possiamo passare la funzione maggiore() per ordinare in ordine decrescente. Questa funzione esegue il confronto in modo da anteporre elementi maggiori.
CPP
// C++ program to sort a vector in non-increasing> // order.> #include> using> namespace> std;> > int> main()> {> > vector<> int> >v{ 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };> > > sort(v.begin(), v.end(), greater<> int> >());> > > cout <<> 'Sorted
'> ;> > for> (> auto> x : v)> > cout << x <<> ' '> ;> > > return> 0;> }> |
>
>Produzione
Sorted 9 8 7 6 5 4 3 2 1 0>
Come ordinare in a ordine particolare?
Possiamo anche scrivere la nostra funzione comparatrice e passarla come terzo parametro.
La funzione comparatrice controlla se l'istruzione restituita è vera o falsa e restituisce un valore bool che viene passato alla funzione di ordinamento.
Ad esempio, supponiamo che Intervallo i1 = { 6 , 8 } e Intervallo i2 = { 1, 9 }. Quando questo viene passato alla funzione comparatore, esegue il confronto i1.inizio E i2.start . Da allora, i1.start (=6)
CPP
// A C++ program to sort vector using> // our own comparator> #include> using> namespace> std;> > // An interval has start time and end time> struct> Interval {> > int> start, end;> };> > // Compares two intervals according to starting times.> bool> compareInterval(Interval i1, Interval i2)> {> > return> (i1.start } int main() { vector v { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } }; // sort the intervals in increasing order of // start time sort(v.begin(), v.end(), compareInterval); cout << 'Intervals sorted by start time :
'; for (auto x : v) cout << '[' << x.start << ', ' << x.end << '] '; return 0; }> |
>
>Produzione
Intervals sorted by start time : [1, 9] [2, 4] [4, 7] [6, 8]>
Come ordinare l'array in ordine decrescente in base ad alcuni parametri utilizzando una funzione comparatrice?
Una funzione comparatrice può essere passata in modo tale che gli elementi nell'array vengano ordinati in ordine decrescente.
C++
// A C++ program to sort vector using> // our own comparator> #include> using> namespace> std;> > // An interval has start time and end time> struct> Interval {> > int> start, end;> };> > // Compares two intervals according to ending times in descending order.> bool> compareInterval(Interval i1, Interval i2)> {> > return> (i1.end>i2.fine);> }> > int> main()> {> > vector v { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };> > > // sort the intervals in decreasing order of> > // end time> > sort(v.begin(), v.end(), compareInterval);> > > cout <<> 'Intervals sorted by ending time in descending order :
'> ;> > for> (> auto> x : v)> > cout <<> '['> << x.start <<> ', '> << x.end <<> '] '> ;> > > return> 0;> }> |
>
>Produzione
Intervals sorted by ending time in descending order : [1, 9] [6, 8] [4, 7] [2, 4]>
Articoli Correlati :
Ordinamento di un vettore di coppie | Insieme 1
Ordinamento di un vettore di coppie | Insieme 2