优先队列映射

Map of priority queues

本文关键字:映射 优先队列      更新时间:2023-10-16

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

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

那么我如何将myType插入到优先级队列中,将其插入到映射中的pair中?在将元素插入到正确的队列之前,我必须进行多次检查,因此了解这一点非常有帮助。

谢谢

// 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,则每次都会产生查找成本,因此最好先将其存储在引用中,然后再使用该引用。