En C++, priority_queue implementa heap . A continuación se muestran algunos ejemplos de cómo crear una cola de prioridad de tipo par .
Cola de prioridad máxima (o montón máximo) ordenada por el primer elemento
// C++ program to create a priority queue of pairs. // By default a max heap is created ordered // by first element of pair. #include <bits/stdc++.h> 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<pair<int, int> > 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; }
Producción :
20 100
Cola de prioridad mínima (o montón mínimo) ordenada por el primer elemento
// C++ program to create a priority queue of pairs. // We can create a min heap by passing adding two // parameters, vector and greater(). #include <bits/stdc++.h> 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<pi, vector<pi>, greater<pi> > 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; }
Producción :
10 200