Conditions préalables : std :: trier en C++ , vecteur en C++ , initialiser un vecteur en C++ .
RPC
// 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;> }> |
>
while boucle java
>Sortir
Sorted 0 1 2 3 4 5 6 7 8 9>
Comment trier par ordre décroissant ?
sort() prend un troisième paramètre utilisé pour spécifier l'ordre dans lequel les éléments doivent être triés. Nous pouvons passer la fonction Greater() pour trier par ordre décroissant. Cette fonction effectue la comparaison de manière à placer les éléments les plus importants avant.
RPC
télécharger sts
// 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;> }> |
>
>Sortir
Sorted 9 8 7 6 5 4 3 2 1 0>
Comment trier dans un ordre particulier?
Nous pouvons également écrire notre propre fonction de comparaison et la transmettre comme troisième paramètre.
La fonction de comparaison vérifie si l'instruction renvoyée est vraie ou fausse et renvoie une valeur booléenne qui est transmise à la fonction de tri.
Par exemple, disons Intervalle i1 = { 6 , 8 } et Intervalle i2 = { 1, 9 }. Lorsque ceci est transmis à la fonction de comparaison, elle compare i1.démarrer et i2.démarrer . Depuis, i1.start (=6)
RPC
théorie des automates
// 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; }> |
>
>
arbre binaire javaSortir
Intervals sorted by start time : [1, 9] [2, 4] [4, 7] [6, 8]>
Comment trier le tableau par ordre décroissant en fonction d'un paramètre à l'aide d'une fonction de comparaison ?
Une fonction de comparaison peut être transmise de telle manière que les éléments du tableau soient triés par ordre décroissant.
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.end);> }> > 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;> }> |
>
machine à états finis
>Sortir
Intervals sorted by ending time in descending order : [1, 9] [6, 8] [4, 7] [2, 4]>
Articles Liés :
Tri d'un vecteur de paires | Ensemble 1
Tri d'un vecteur de paires | Ensemble 2