如何配置 std::priority_queue 以忽略重复项?
如何配置 std::priority_queue 来忽略重复项?
当我添加一个已包含的密钥时,应该忽略这个新密钥。 (就我而言,旧的和新的优先级将始终完全相同。)
从复杂性角度来看,它不应该有什么区别:它将尝试插入到适当的位置,找到现有的位置,并且不执行任何操作。问题只是 std::priority_queue 是否可以以这种方式配置。
How can I configure std::priority_queue
to ignore duplicates?
When I add a key that is already contained then this new one should be ignored. (In my case, the priority for the old and the new one will always be exactly the same.)
Complexity-wise it should not make a difference: It will try to insert at the appropriate location, find the existing one there and do nothing. The question is just if std::priority_queue
is configurable in that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以从 STL 集中实现priority_queue。
实现可迭代的优先级队列C++
You can implement a priority_queue out of an STL set.
Implementing a priority queue that can be iterated over in C++
正如 Aater 提到的,您可以使用 stl 集作为优先级队列。
另一种选择是将重复项添加到优先级队列中,但是当从队列中弹出项目时,请跟踪先前的值并忽略重复的值。例如,这里处理优先级队列上的所有值,同时忽略重复项
As Aater mentioned you could use an stl set as a priority queue.
Another option is to add duplicates to the priority queue, but when popping items off the queue, keep track of the previous value and ignore repeated values. For example, here's processing all values on a priority queue while ignoring duplicates