使用向量C++的双精度的优先级队列

Priority queue of doubles using vector C++

本文关键字:双精度 队列 优先级 C++ 向量      更新时间:2023-10-16

我需要使用 STL 向量为优先级队列编写代码

我被分配了使用向量实现双精度优先级队列的任务以前我使用 list 实现了队列,这非常简单,因为 list 为我提供了我需要的所有功能。除非我错过了优先级队列不是这种情况的东西?

cplusplus.com 上优先级队列的参考说明:一次调用底层容器(向量)上的push_back,一次调用push_heap(我假设算法),我不确定如何实现push_heap和pop_heap,或者我是否需要

作业专门要求向量

我正在寻找一个很好的实现/解释。

这是到目前为止的mo代码:

。.cpp

#include"PriorityQueue.h"
#include <algorithm>
PriorityQueue::PriorityQueue(){
}
void PriorityQueue::push(double val){
    s.push_back(val);
}
void PriorityQueue::pop(){

}
double PriorityQueue::front() const{
    return s.front();
}
double PriorityQueue::back() const{
    return s.back();
}

.h

#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
#include <vector>
using std::vector;
class PriorityQueue{
public:
    PriorityQueue();
    void push(double val);
    void pop();
    double front() const;
    double back() const;
private:
    vector<double> s;
};
#endif // !PRIORITYQUEUE_H

如果我需要编写代码来跟踪顶部和底部索引,我可能会缺少基本功能,我只是有点卡住了,没有时间浪费尝试大量不同的解决方案。

您可以简单地使用std::vector,并使用push_backalgorithm标头中的std::push_heap相结合向其添加项目。