# Priority queue of pairs in C++ (Ordered by first)

In C++, priority_queue implements heap. Below are some examples of creating priority queue of pair type.

Max Priority queue (Or Max heap) ordered by first element

 `// C++ program to create a priority queue of pairs. ` `// By default a max heap is created ordered ` `// by first element of pair. ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// Driver program to test methods of graph class ` `int` `main() ` `{ ` `    ``// By default a max heap is created ordered ` `    ``// by first element of pair. ` `    ``priority_queue > pq; ` ` `  `    ``pq.push(make_pair(10, 200)); ` `    ``pq.push(make_pair(20, 100)); ` `    ``pq.push(make_pair(15, 400)); ` ` `  `    ``pair<``int``, ``int``> top = pq.top(); ` `    ``cout << top.first << ``" "` `<< top.second; ` `    ``return` `0; ` `} `

Output :

`20 100`

Min Priority queue (Or Min heap) ordered by first element

 `// C++ program to create a priority queue of pairs. ` `// We can create a min heap by passing adding two  ` `// parameters, vector and greater(). ` `#include ` ` `  `using` `namespace` `std; ` ` `  `typedef` `pair<``int``, ``int``> pi; ` ` `  `// Driver program to test methods of graph class ` `int` `main() ` `{ ` `    ``// By default a min heap is created ordered ` `    ``// by first element of pair. ` `    ``priority_queue, greater > pq; ` ` `  `    ``pq.push(make_pair(10, 200)); ` `    ``pq.push(make_pair(20, 100)); ` `    ``pq.push(make_pair(15, 400)); ` ` `  `    ``pair<``int``, ``int``> top = pq.top(); ` `    ``cout << top.first << ``" "` `<< top.second; ` `    ``return` `0; ` `} `

Output :

`10 200`

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : addiegupta

Article Tags :
Practice Tags :

6

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.