优先级队列的映射

时间:2014-03-29 19:25:03

标签: c++ priority-queue unordered-map

我正在尝试编写一对优先级队列的映射。 在将其添加到地图之前,我真的不确定如何初始化和元素。 特别是当该对不存在并且我必须创建它然后填充一个元素时,将一个元素排队到正确的队列中,然后将整个对插入到映射中。

typedef pair<priority_queue<myType>, priority_queue<myType>> Queue_Pair;
typedef unordered_map<string, Queue_Pair>  Map_of_Queues;
Map_of_Queues myMap;

那么如何将myType插入优先级队列成对映射? 在将元素插入正确的队列之前,我将不得不进行多项检查,因此这对我们来说非常有用。

由于

1 个答案:

答案 0 :(得分:1)

// Get a reference to the Queue_Pair associated with "key"
// If it doesn't yet exist, create it.
Queue_Pair& qp = myMap["key"];

// add an element to the first priority queue
qp.first.push(myType_object);

// add an element to the second priority queue
qp.second.push(another_myType_object);

请注意,您可以这样做:

myMap["key"].first.push(myType_object);

但是,如果您要按顺序多次重复使用关联的Queue_Pair,则每次都会产生查找成本,因此最好先将其存储在引用中,然后再使用该引用。 / p>