本文介绍了如何合并两个priority_queue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 priority_queue float ,如下所示:

I have two priority_queue with float like this:

std::priority_queue<float> queue1;
std::priority_queue<float> queue2;

我需要合并它们。但是STL merge 算法不允许直接使用 priority_queue

And I need to merge them. But STL merge algorithm do not allow working with the priority_queue directly:

merge(
  queue1.begin(), queue2.end(),
  queue2.begin(), queue2.end(),
  queue1
);

有没有办法合并 priority_queue 使用辅助数据结构?

Is there a way to merge priority_queue without using auxiliary data structures?

推荐答案

priority_queue 是容器适配器常规标准容器。特别是,它不提供 begin() end()成员函数。因此,您必须从一个队列中弹出元素,并将它们推入另一个:

priority_queue is a container adapter, not a regular standard container. In particular, it does not offer the begin() and end() member functions. Therefore, you will have to pop the elements out of one queue and push them into the other:

while (!queue2.empty())
{
    queue1.push(queue2.top());
    queue2.pop();
}

这篇关于如何合并两个priority_queue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 18:31