Alternative way to sort a vector
This commit is contained in:
committed by
Kristofer Koishigawa
parent
2efc5a8e7b
commit
d4848df011
@ -214,6 +214,7 @@ int main()
|
||||
```
|
||||
### Sorting Vector In Descending Order
|
||||
Sorting Vector in descending order can be done with the help of third argument namely greater<int>() in Sort() in C++.
|
||||
|
||||
``` cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
@ -233,5 +234,26 @@ int main(){
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
An alternative way to do this.
|
||||
|
||||
``` cpp
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
|
||||
vector<int> v{ 10, 5, 82, 69, 64, 70, 3, 42, 28, 0 };
|
||||
sort(v.rbegin(), v.rend());
|
||||
|
||||
cout << "Vector Contents Sorted In Ascending Order:\n";
|
||||
for(int e : v){
|
||||
cout << e << " ";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
You can also sort in descending using lambda like the one above.
|
||||
|
Reference in New Issue
Block a user