简体   繁体   English

STL可迭代容器,例如priority_queue

[英]STL iterable container like priority_queue

I'm new to STL containers (and C++ in general) so thought I would reach out to the community for help. 我是STL容器(和C ++)的新手,所以我想向社区寻求帮助。 I basically want to have a priority_queue that supports constant iteration. 我基本上想拥有一个支持常量迭代的priority_queue Now, it seems that std::priority_queue doesn't support iteration, so I'm going to have to use something else, but I'm not sure exactly what. 现在,似乎std::priority_queue不支持迭代,因此我将不得不使用其他东西,但是我不确定到底是什么。

Requirements: 要求:

  • Maintains order on insertion (like a priority queue) 保持插入顺序(例如优先级队列)
  • Pop from top of list 从列表顶部弹出
  • Get const access to each element of the list (don't care about the order in the queue for this stage) 获得对列表中每个元素的const访问权限(此阶段无关队列中的顺序)

One option would be to keep a priority_queue and separately have an unordered_set of references, but I'd rather not have two containers floating around. 一种选择是保留一个priority_queue并分别具有一个unordered_set的引用,但是我宁愿不要有两个浮动的容器。 I could also use a deque and search through for the right insertion position, but I'd rather have the container manage the sorting for me if possible (and constant-time insertion would be nicer than linear-time). 我也可以使用deque并搜索正确的插入位置,但我宁愿让容器为我管理排序(并且恒定时间插入比线性时间更好)。 Any suggestions? 有什么建议么?

There are two options that come to mind: 有两种选择:

1) Implement your own iterable priority queue, using std::vector and the heap operation algorithms (see Heap Operations here ). 1)实现你自己的迭代优先级队列,使用std::vector和堆操作算法(参见堆操作 在这里 )。

2) derive (privately) from priority_queue . 2)(私下)从priority_queue派生。 This gives you access to the underlying container via data member c . 这使您可以通过数据成员c访问底层容器。 You can then expose iteration, random access, and other methods of interest in your public interface. 然后,您可以在公共接口中公开迭代,随机访问和其他感兴趣的方法。

Using a std::vector might be enough as others already pointed, but if you want already-ready implementation, maybe use Boost.Heap (which is a library with several priority queue containers): http://www.boost.org/doc/libs/1_53_0/doc/html/heap.html 使用std :: vector可能已足够,正如其他人已经指出的那样,但是如果您想已经准备好实现,则可以使用Boost.Heap(这是一个具有多个优先级队列容器的库): http : //www.boost.org/ doc / libs / 1_53_0 / doc / html / heap.html

Boost is a collection of libraries that basically complete the standard library (which is not really big). Boost是基本完成标准库(不是很大)的库的集合。 A lot of C++ developers have boost ready on their dev computer to use it when needed. 许多C ++开发人员已经准备好在其开发计算机上使用它们,以便在需要时使用它。 Just be careful in your choices of libraries. 选择库时要小心。

You can use (ordered) set as a queue. 您可以将(有序)设置为队列。 set.begin() will be your top element, and you can pop it via erase(set.begin()). set.begin()将是您的顶部元素,您可以通过delete(set.begin())将其弹出。

Have you observed heap ( std::make_heap ) ? 您是否观察到了堆( std :: make_heap )? It hasn't order inside of queue, but has priority "pop from top of list" which you need. 它没有在队列中排序,但是具有您需要的优先级“从列表顶部弹出”。

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

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