正在创建STL最小堆优先级队列
Creating a STL min heap priority queue?
我的任务是实现一个最小堆优先级队列,用m_time成员变量对每个队列对象进行排序。我的问题是,我无法管理队列以先排序最小而不是最大。
我在.h文件中有一个名为Event的结构,它包括三个变量:
struct Event
{
Event(int time=-1, int grind=-1, bool val=false)
{ m_time=time;m_grindNr=grind; m_value=val;}
int m_time;
int m_grindNr;
bool m_value;
};
下面的代码是.cpp文件中的内容:
struct compare
{
bool operator()(const Event& a, const Event& b)
{
return a.m_time < b.m_time;
}
};
void main()
{
priority_queue <Event,vector<Event>, compare> Que;
Event firstEvent;
firstEvent.m_time = 2;
firstEvent.m_grindNr = 0;
firstEvent.m_value = 0;
Que.push(firstEvent);
Event secondEvent;
secondEvent.m_time = 5;
secondEvent.m_grindNr = 0;
secondEvent.m_value = 0;
Que.push(secondEvent);
Event tempEvent = Que.top(); //Takes the top value
Que.pop();
cout << tempEvent.m_time << " "; //Should print number 2, but prints 5
tempEvent = Que.top(); //Takes the top value
Que.pop();
cout << tempEvent.m_time << endl; //Should print number 5, but prints 2
}
我还尝试在优先级队列参数中使用std::less,但结果相同。
我希望你能理解我的问题,提前谢谢。
您必须使用greater
,因为priority_queue首先获取最大。
因此,将您的比较更改为
return b.m_time < a.m_time;
相关文章:
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 打印优先级队列
- 带自定义比较器的最小优先级队列
- 优先级队列自定义比较器
- 排序数组优先级队列
- 更改运行时优先级队列的排序功能
- 优先级队列构造函数的工作
- 实现优先级队列
- 优先级队列功能比较
- 在C++中打印对的优先级队列的所有值时出现问题
- 使用堆的优先级队列,具有相同键的值不遵循 FIFO(先进先出)
- 为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?
- 是否可以使用简单队列创建优先级队列
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- Cython中带有自定义比较器的优先级队列
- 优先级队列比较器[C++].
- asio::io_service 具有多个线程的优先级队列处理