准确地说,我正在尝试编写一个优先级队列对的映射。 在将它添加到 map 之前,我真的不确定如何初始化和元素。 特别是当该对不存在并且我必须创建它然后填充一个元素队列将一个元素放入正确的队列中然后将整个对插入 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 插入到优先级队列中成对映射到 map 中呢? 在将元素插入正确的队列之前,我将不得不进行多次检查,因此了解这一点真的很有帮助。
谢谢
最佳答案
// 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
,这将导致每次查找成本,所以最好先将它存储在一个引用中,然后再使用它引用。
关于c++ - 优先队列图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22735779/