-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorPAIR.cpp
62 lines (41 loc) · 1.21 KB
/
vectorPAIR.cpp
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
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
/* 1. iniitalize a pair
2. cout the pair
3. sort the pair and then cout
*/
int main(void){
vector<pair<int,int>> number;
int n;
cin >> n; // to input the number of tuples
for(int i=0; i<n; i++){
int value1,value2;
cin >> value1 >> value2;
number.push_back(make_pair(value1,value2));
}
cout << endl;
//To diplsay out the pair
for(int i=0; i<n; i++){
cout << get<0>(number[i]) << " " // first element
<< get<1>(number[i]) << "\n"; // second element
}
// using lamda expression!!
//ASCENDING '<'
//DESCENDING '>'
sort(number.begin(), number.end(), [] (const pair<int,int>& x, const pair<int,int>& y){
//compare 2nd element
if (get<1>(x) != get<1>(y)){ //DEPENDS HOW U WANNA SORT. COMPARE 1ST ELEMENT OR 2ND ELEMENT first
return (get<1>(x) < get<1>(y));
}
//compare 1st element if 2nd equal
else
return (get<0>(x) < get<0>(y));
});
cout << endl;
//To diplsay out the pair
for(int i=0; i<n; i++){
cout << get<0>(number[i]) << " " // first element
<< get<1>(number[i]) << "\n"; // second element
}
return 0;
}