How I can find value in priority queue?(如何在优先队列中找到价值?)
问题描述
我想在我的优先队列中找到一个节点,但我没有找到解决方案 :(如果你有解决方案,我很感兴趣.
I would like to find a node in my priority queue but I did not find a solution :( If you have a solution, I'm interested.
谢谢帮助.
推荐答案
如果你真的需要搜索一个 std::priority_queue 并且想要高效地完成它,你可以派生一个新类并添加find 成员函数.由于您没有添加任何其他状态,因此您不必担心切片或其他问题,因为 std::priority_queue 不是多态的.
If you really need to search through a std::priority_queue and want to do it efficiently you can derive a new class and add a find member function. Since you are not adding any additional state you do not have to worry about slicing or other issues since std::priority_queue is not polymorphic.
#include <queue>
template<
class T,
class Container = std::vector<T>,
class Compare = std::less<typename Container::value_type>
> class MyQueue : public std::priority_queue<T, Container, Compare>
{
public:
typedef typename
std::priority_queue<
T,
Container,
Compare>::container_type::const_iterator const_iterator;
const_iterator find(const T&val) const
{
auto first = this->c.cbegin();
auto last = this->c.cend();
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
};
这篇关于如何在优先队列中找到价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在优先队列中找到价值?
基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
