-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTL_ALgorithms.cpp
More file actions
52 lines (45 loc) · 1.41 KB
/
STL_ALgorithms.cpp
File metadata and controls
52 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool comparator(pair<int, int> p1, pair<int, int> p2){ //Custom Comparator
if (p1.second<p2.second) return true;
if (p1.second>p2.second) return false;
if (p1.first<p2.first) return true;
else return false;
}
int main(){
int arr[]={2,4,1,6,7}; //Sort arrays
int n=sizeof(arr)/sizeof(arr[0]);
reverse(arr,arr+n); //Reverse array
for (int i=0 ; i< n ; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
sort(arr,arr+n);
for (int i=0 ; i< n ; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
sort(arr,arr+n ,greater<int>() ); //Sort in descending order
for (int i=0 ; i< n ; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
vector<int> vect={1,2,8,6,4}; //Sort vectors
int size=vect.size();
sort(vect.begin(),vect.end());
for (int val: vect){
cout<<val<<" ";
}
cout<<endl;
vector<pair<int, int>> vec={{3,1},{2,1},{7,1},{5,2}};
sort(vec.begin(),vec.end(),comparator); //Custtom Comparator passed
for (auto p: vec){
cout<<p.first<<" "<<p.second<<endl;
}
string s="abc"; //Next permutation
next_permutation(s.begin(),s.end());
cout<<"Next Permutation: "<<s;
return 0;
}