# Priority Queue of Pairs in C++ with an example

In this tutorial, we will learn about the priority queue of pairs in C++. We will understand this along with a suitable example.

## Priority Queue of pairs

A priority queue is an abstract data type in which every element has a priority associated with it. So, all the operations are performed first on the element with the highest priority. Priority queue of pairs means each element is a pair of elements.

#### Syntax:

priority_queue <pair <data_type, data_type> > identifier;

While pushing the elements into the queue we need to use the function make_pair() because each element is a pair of elements.

### Example in C++

Let us include the queue header file. We need this for handling priority queues.

```#include <iostream>
#include <queue>
using namespace std;```

Now let us create a priority queue where both the elements of the pair are integers.

```int main()
{
priority_queue <pair <int, int> > priorityQ;
```

Let us push some elements into the queue.

```priorityQ.push (make_pair (6, 350));
priorityQ.push (make_pair (8, 168));
priorityQ.push (make_pair (4, 289));
priorityQ.push (make_pair (9, 712));
```

By default, the first element of the pair is used for prioritizing and a max heap gets constructed with the given elements. So, the element with highest value is at the front/top of the queue. Let us create a variable and witness this.

```pair<int, int> top = priorityQ.top();
cout << top.first << " " << top.second;```

We retrieved the top element using the top() function. We can retrieve the first and second elements as variable_name.first and variable_name.second.

As mentioned earlier any operation is performed on the element with the highest priority. Let us perform the pop() operation to check this out.

```priorityQ.pop();
top = priorityQ.top();
cout << endl << top.first << " " << top.second;

return 0;
}```

We observe that this is true.

Output:

```9 712
8 168```

Also read, How to implement Priority Queue in C++