61 lines
1.3 KiB
C++
61 lines
1.3 KiB
C++
//
|
||
// 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
|