forked from codewithshailendra/COYO-IIT-BHU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFUNCTORS AND PRIORITY QUEUE IN STL IN CPP
86 lines (55 loc) · 1.03 KB
/
FUNCTORS AND PRIORITY QUEUE IN STL IN 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<bits/stdc++.h>
using namespace std;
class car
{
public:
int x;
int y;
int id;
car(int id,int x,int y)
{
this->id=id;
this->x=x;
this->y=y;
}
int distance()
{
return x*x+y*y;
}
void print()
{
cout<<"ID "<<id;
cout<<"LOCATION :"<<x<<" , "<<y<<endl;
}
};
// functors - Functional Objects
class carcompare{
public:
// Method to overload() operator
bool operator()(car a, car b)
{
return a.distance() > b.distance();
}
};
int main()
{
//priority_queue<int,vector<int>,greater<int> >pq;
priority_queue<car,vector<car>,carcompare>pq;
int x[10]={5,6,17,18,9,11,0,3};
int y[10]={1,-2,8,9,10,91,1,2};
//Insert these 8 cars in the priority queue.
for(int i=0;i<8;i++)
{
car c(i,x[i],y[i]);
pq.push(c);
}
// Heap-max!!
int poppings=1;
while(!pq.empty() && poppings<=3)
{
car p=pq.top();
p.print();
pq.pop();
poppings++;
}
}