简体   繁体   English

列出优先级队列

[英]List to priority queue

I have a college programming project in C++ divided into two parts. 我在C ++中有一个大学编程项目,分为两部分。 I beggining the second part where it's supposed to use priority_queues , hash tables and BST 's. 我开始第二部分,它应该使用priority_queueshash tablesBST

I'm having trouble (at least) with priority queues since it's obligating myself to redone a lot of code already implemented in the first part. 我(至少)遇到了优先级队列的麻烦,因为它有义务重做已经在第一部分中实现的许多代码。

The project it's about implementing a simple airport management system and, therefore, I have classes like Airport (main class), Airplane, Terminal and Flight. 该项目是关于实施一个简单的机场管理系统 ,因此,我有类似机场(主要类),飞机,终端和飞行等课程。 My airport had a list of terminals but now the project specification points out that I must keep the terminals in a priority_queue where the top contains the terminal less occupied, ie has less flights. 我的机场有一个终端list ,但现在项目规范指出我必须将终端保持在 priority_queue ,其中顶部包含终端占用较少,即航班较少。

For each class, I have CRUD functions but now how am I supposed, for example, edit a terminal and add a flight to it? 对于每个班级,我有CRUD功能,但现在我怎么想,例如,编辑一个终端并添加一个航班? With a list, I just had to iterate to a specific position but now I only have access to object in the top of the queue. 使用列表,我只需要迭代到一个特定的位置,但现在我只能访问队列顶部的对象。 The solution I thought about was to copy the priority queue terminals to a temporary list but, honestly, I don't like this approach. 我想到的解决方案是将优先级队列终端复制到临时列表,但老实说,我不喜欢这种方法。

What should I do? 我该怎么办?

Thanks in advance. 提前致谢。

It sounds like you need a priority queue with efficient increase and decrease key operations. 听起来你需要一个有效增加和减少关键操作的优先级队列。 You might be better of creating you own your own priority queue implementation. 您可能最好创建自己的优先级队列实现。

The priority_queue container is great for dynamic sets. priority_queue容器非常适合动态集。 But since the number of terminal in an airport are pretty much fixed you can a fixed size container with the heap family of algorithms. 但是,由于机场中的终端数量相当固定,因此可以使用具有堆算法系列的固定大小的容器。

As the internal storage, you could use any container that provides random access iterators (vector, array, deque). 作为内部存储,您可以使用任何提供随机访问迭代器(vector,array,deque)的容器。 Then, use make_heap(), sort_heap() family of functions to heapify the array. 然后,使用make_heap(),sort_heap()函数系列来堆积数组。 Now you can cheaply access the top(), modify the priority of a random member in the heap and iterate through all elements easily. 现在,您可以廉价地访问top(),修改堆中随机成员的优先级,并轻松遍历所有元素。

For an example see: http://www.cplusplus.com/reference/algorithm/make_heap/ 有关示例,请参阅: http//www.cplusplus.com/reference/algorithm/make_heap/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM