This commit is contained in:
e2hang
2025-08-09 19:23:37 +08:00
parent ac48a86396
commit 17105e9e9e
13 changed files with 194 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
//
// Created by PC on 25-8-9.
//
#ifndef MAXPRIORITYQUEUEREALISE_H
#define MAXPRIORITYQUEUEREALISE_H
#include "maxPriorityQueue.h"
#include <vector>
template <class T>
class maxPriorityQueueRealise : public maxPriorityQueue<T> {
private:
std::vector<std::pair<T, int>> queue; //T是元素int是优先等级
int qsize;
public:
maxPriorityQueueRealise() : qsize(0) {}
bool empty() const {return qsize == 0;}
int size() const {return qsize;}
T& top();
void pop();
void push(const std::pair<T, int>& x);
};
template<class T>
T& maxPriorityQueueRealise<T>::top() {
int max = -1;
int pos = 0;
int index = 0;
for ( std::pair<T, int> x : queue ) {
if (x.second > max) {
max = x.second;
pos = index;
}
index++;
}
return queue[pos].first;
}
template<class T>
void maxPriorityQueueRealise<T>::pop() {
int max = -1;
int pos = 0;
int index = 0;
for ( std::pair<T, int> x : queue ) {
if (x.second > max) {
max = x.second;
pos = index;
}
index++;
}
queue.erase(queue.begin() + pos);
}
template<class T>
void maxPriorityQueueRealise<T>::push(const std::pair<T, int>& x) {
queue.push_back(x);
}
#endif //MAXPRIORITYQUEUEREALISE_H