列表到优先队列

发布于 2024-10-07 10:44:58 字数 502 浏览 6 评论 0原文

我有一个 C++ 大学编程项目,分为两个部分。我开始第二部分,它应该使用 priority_queues哈希表BST

我(至少)在优先级队列方面遇到了麻烦,因为它迫使我自己重做第一部分中已经实现的许多代码。

该项目是关于实现一个简单的机场管理系统,因此,我有机场(主类)、飞机、航站楼和航班等类。我的机场有一个航站楼列表,但现在项目规范指出我必须将航站楼保留在 priority_queue 中,其中顶部包含航站楼占用较少,即航班较少。

对于每个类,我都有 CRUD 功能,但现在我该怎么办,例如,编辑一个航站楼并向其添加一个航班?使用列表,我只需迭代到特定位置,但现在我只能访问队列顶部的对象。我想到的解决方案是将优先级队列终端复制到临时列表,但说实话,我不喜欢这种方法。

我应该怎么办?

提前致谢。

I have a college programming project in C++ divided into two parts. I beggining the second part where it's supposed to use priority_queues, hash tables and BST's.

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, i.e has less flights.

For each class, I have CRUD functions but now how am I supposed, for example, edit a terminal and add a flight to it? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-10-14 10:44:58

听起来您需要一个具有高效增加和减少键操作的优先级队列。您最好创建自己的优先级队列实现。

Priority_queue 容器非常适合动态集。但由于机场中航站楼的数量几乎是固定的,因此您可以使用堆系列算法来使用固定大小的容器。

作为内部存储,您可以使用任何提供随机访问迭代器的容器(向量、数组、双端队列)。然后,使用 make_heap()、sort_heap() 系列函数来堆化数组。现在,您可以廉价地访问 top()、修改堆中随机成员的优先级并轻松地迭代所有元素。

示例请参见:
http://www.cplusplus.com/reference/algorithm/make_heap/

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. 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). Then, use make_heap(), sort_heap() family of functions to heapify the array. Now you can cheaply access the top(), modify the priority of a random member in the heap and iterate through all elements easily.

For an example see:
http://www.cplusplus.com/reference/algorithm/make_heap/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文