2014-03-29 29 views
0

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

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
// 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多次重复使用,这将招致查找每个成本时间,因此最好先将它存储在一个引用中,然后使用该引用。