fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct node{
  4. int x;
  5. int y;
  6. int z;
  7. };
  8.  
  9. struct comparator{
  10. //using operator overloading
  11. bool operator()(node &n1, node &n2){
  12. int sum1=n1.x+n1.y+n1.z;
  13. int sum2=n2.x+n2.y+n2.z;
  14. //ordering
  15. if(sum1>sum2)
  16. return true;
  17. else
  18. return false;
  19. }
  20. };
  21. int main(){
  22. int arr[]={2,3,6,5,1};
  23. int n=5;
  24. //max-heap
  25. priority_queue<int>pq1;
  26. for(int i=0;i<n;i++){
  27. pq1.push(arr[i]);
  28. }
  29. cout<<"Max-heap\n";
  30. while(!pq1.empty()){
  31. int value=pq1.top();
  32. pq1.pop();
  33. cout<<value<<endl;
  34. }
  35.  
  36. //min-heap
  37. priority_queue<int,vector<int>,greater<int>>pq2;
  38. for(int i=0;i<n;i++){
  39. pq2.push(arr[i]);
  40. }
  41. cout<<"Min-heap\n";
  42. while(!pq2.empty()){
  43. int value=pq2.top();
  44. pq2.pop();
  45. cout<<value<<endl;
  46. }
  47.  
  48.  
  49. //using custom data type
  50. priority_queue<node,vector<node>,comparator>pq3;
  51. for(int i=0;i<n;i++){
  52. int x,y,z;
  53. cin>>x>>y>>z;
  54. pq3.push(node{x,y,z});
  55. }
  56. cout<<"Using Custom datatype\n";
  57. while(!pq3.empty()){
  58. node temp=pq3.top();
  59. pq3.pop();
  60. cout<<temp.x<<" "<<temp.y<<" "<<temp.z<<endl;
  61. }
  62.  
  63. }
  64.  
Success #stdin #stdout 0s 4392KB
stdin
1 2 3
1 2 4
1 2 5
1 2 6
2 2 7
stdout
Max-heap
6
5
3
2
1
Min-heap
1
2
3
5
6
Using Custom datatype
1 2 3
1 2 4
1 2 5
1 2 6
2 2 7