使用向量C++的双精度的优先级队列
Priority queue of doubles using vector C++
我需要使用 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_back
与algorithm
标头中的std::push_heap
相结合向其添加项目。
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 在 C++ 中将双精度变量写入二进制文件
- 如何从字符串转换为双精度*
- 为什么我的数组双精度函数不起作用?
- 高精度双精度的 Sprintf 格式化问题
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 将指针数组分配给双精度
- C++如何将字符串逐行转换为双精度
- 使用向量C++的双精度的优先级队列