How to iterate over a priority_queue?(如何迭代priority_queue?)
问题描述
我可以使用迭代器(如 vector
)在 C++ 中遍历标准 priority_queue
或标准 queue
吗?我不想使用 pop,因为它会导致我的队列出队.
Can I traverse a standard priority_queue
or standard queue
in c++ with an iterator (like a vector
)? I don't want to use pop because it cause my queue to be dequeued.
感谢您的帮助
推荐答案
priority_queue
不允许遍历所有成员,大概是因为太容易使队列的优先级排序无效(通过修改您遍历的元素)或者这可能是不是我的工作"的理由.
priority_queue
doesn't allow iteration through all the members, presumably because it would be too easy in invalidate the priority ordering of the queue (by modifying the elements you traverse) or maybe it's a "not my job" rationale.
官方的解决方法是使用 vector
代替,并使用 make_heap
、push_heap
和 自己管理优先级pop_heap
.在@Richard 的回答中,另一种解决方法是使用从 priority_queue
派生的类,并访问具有 protected
可见性的底层存储.
The official work-around is to use a vector
instead and manage the priority-ness yourself with make_heap
, push_heap
and pop_heap
. Another work-around, in @Richard's answer, is to use a class derived from priority_queue
and access the underlying storage which has protected
visibility.
这篇关于如何迭代priority_queue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何迭代priority_queue?


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09