Sorting from small to large is relatively simple , use sort() Function can ,sort The header file is #include<algorithm>

from small to large
#include <iostream> #include <vector> #include <algorithm> using namespace
std; int main() { int a[] = {8,6,2,9,3,5,4,1,7,10}; vector<int> arr(a, a+5);
sort(arr.begin(),arr.end()); for(int i = 0; i <arr.size(); ++i){ cout <<arr[i]
<< " "; } return 0 ; }
From big to small

Method 1 ：sort The default sort is from small to large , But you can write your own functions or use them greater<int>(), Or customize the collation
#include <iostream> #include <vector> #include <algorithm> using namespace
std; int main() { int a[] = {8,6,2,9,3,5,4,1,7,10}; vector<int> arr(a, a+5);
sort(arr.begin(),arr.end(),greater<int>()); for(int i = 0; i <arr.size(); ++i){
cout <<arr[i] << " "; } return 0 ; }
Or define your own function , The big one is written in front
#include <iostream> #include <vector> #include <algorithm> using namespace
std; bool cmp_max(int x,int y){ return x > y; } int main() { int a[] =
{8,6,2,9,3,5,4,1,7,10}; vector<int> arr(a, a+5);
sort(arr.begin(),arr.end(),cmp_max); for(int i = 0; i <arr.size(); ++i){ cout
<<arr[i] << " "; } return 0 ; }
Method 2 ： use sort After sorting , use reverse()

notes ：reverse()  Invert elements , But not arranged
#include <iostream> #include <vector> #include <algorithm> using namespace
std; int main() { int a[] = {8,6,2,9,3,5,4,1,7,10}; vector<int> arr(a, a+5);
sort(arr.begin(),arr.end()); reverse(arr.begin(),arr.end()); for(int i = 0; i
<arr.size(); ++i){ cout <<arr[i] << " "; } return 0 ; }

Technology
Daily Recommendation
views 9
views 8