源码:
PriorityQueue.h
#pragma once
#ifndef __PRIORITY_QUEUE_H__
#define __PRIORITY_QUEUE_H__
#include"Heap.h"
template<class T>
class PriorityQueue
{
public:
PriorityQueue()
{}
//插入
void Push(const T& data){
_hp.Push(data);
}
//删除
void Pop(){
_hp.Pop();
}
//判断是否为空
bool Empty(){
return _hp.Empty();
}
//获得优先级最高元素
T Top(){
return _hp.Top();
}
//队列元素的个数
size_t Size(){
return _hp.Size();
}
private:
Heap<T> _hp;
};
#endif //__PRIORITY_QUEUE_H__
test.cpp
#include"Heap.h"
#include"PriorityQueue.h"
#include<iostream>
#include<stdio.h>
#include<Windows.h>
using namespace std;
//堆
void test(){
int arr[] = { 53, 17, 78, 9, 45, 65, 87, 23 };
Heap<int,Less<int>> hp(arr, sizeof(arr) / sizeof(arr[0]));
hp.Push(80);
hp.Pop();
cout << hp.Size() << endl;
cout << hp.Top() << endl;
cout << hp.Back() << endl;
}
//优先级队列
void test2(){
int arr[] = { 53, 17, 78, 9, 45, 65, 87, 23 };
PriorityQueue<int> prq;
for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i){
prq.Push(arr[i]);
}
cout << prq.Top() << endl;
cout << prq.Size() << endl;
prq.Pop();
cout << prq.Top() << endl;
cout << prq.Size() << endl;
}
int main(){
//test();
test2();
system("pause");
return 0;
}
程序测试结果:
插入:
删除:
队头元素&队列元素个数: