Priekšnosacījumi: std::kārtot C++ , vektors C++ valodā , inicializējiet vektoru programmā C++ .
10 ml līdz oz
CPP
// 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;> }> |
>
>Izvade
Sorted 0 1 2 3 4 5 6 7 8 9>
Kā kārtot dilstošā secībā?
sort() aizņem trešo parametru, ko izmanto, lai norādītu secību, kādā elementi ir jākārto. Mēs varam nodot lielāku () funkciju, lai kārtotu dilstošā secībā. Šī funkcija veic salīdzināšanu tādā veidā, kas liek priekšā lielākus elementus.
CPP
kā atjaunināt java
// 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;> }> |
>
>Izvade
Sorted 9 8 7 6 5 4 3 2 1 0>
Kā kārtot a īpašs pasūtījums?
Mēs varam arī uzrakstīt savu salīdzinājuma funkciju un nodot to kā trešo parametru.
Salīdzinājuma funkcija pārbauda, vai atgrieztais paziņojums ir patiess vai nepatiess, un atgriež būtības vērtību, kas tiek nodota kārtošanas funkcijai.
Piemēram, pieņemsim, ka Intervāls i1 = { 6 , 8 } un Intervāls i2 = { 1, 9 }. Kad tas tiek nodots salīdzinājuma funkcijai, tā salīdzina i1.sākt un i2.sākt . Kopš i1.start (=6)
kas ir uri
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; }> |
>
>Izvade
Intervals sorted by start time : [1, 9] [2, 4] [4, 7] [6, 8]>
Kā kārtot masīvu dilstošā secībā, pamatojoties uz kādu parametru, izmantojot salīdzinājuma funkciju?
Salīdzinājuma funkciju var nodot tādā veidā, lai elementi masīvā tiktu sakārtoti dilstošā secībā.
C++
fibonači sērija gadsimtā
// 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;> }> |
>
>Izvade
Intervals sorted by ending time in descending order : [1, 9] [6, 8] [4, 7] [2, 4]>
Saistītie raksti :
Pāru vektora šķirošana | 1. komplekts
Pāru vektora šķirošana | 2. komplekts