一個映射元組使用C++商店參考那裏有地圖這種類型的:在另一個函數
std::map<int, std::tuple<int, std::vector<HlEdgeEntry*>*>> edgesMap;
我現在想存儲的
std::tuple<int, std::vector<HlEdgeEntry*>*>
以內的部分另一個優先隊列。 這樣,從優先級隊列提取時,我不需要運行地圖的.find方法。
這樣我PriorityQueue的定義如下:
typedef std::pair<int, std::tuple<int, std::vector<HlEdgeEntry*>*>>
EdgePairType;
typedef std::pair<int, EdgePairType> QueuePairType;
struct CompareQueueEntry :
public std::binary_function<QueuePairType, QueuePairType, bool>
{
bool operator()
(const QueuePairType firstQueuePair,
const QueuePairType secondQueuePair) const
{
return firstQueuePair.first < secondQueuePair.first;
}
};
typedef std::priority_queue<QueuePairType, vector<QueuePairType>,
CompareQueueEntry> PriorityQueueType;
地圖本身還被使用在所有的代碼。
在另一個(initQueue())函數中插入優先級隊列比選擇下一個優先級(doWork())的位置要多。 這兩種方法都通過引用獲取priorityQueue和map,這兩個方法都在第三個函數controlWork()中定義,並調用前面提到的兩種方法。這看起來像如下:
std::map<int, std::tuple<int, std::vector<HlEdgeEntry*>*>> edgesMap;
PriorityQueueType priorityQueue;
k->initQueue(edgesOrelSource, edgesMap);
k->doWork(edgesMap, priorityQueue);
的問題是,從接收隊列中的元組後(內的doWork()),我想改變的元組值之一,使得它影響withing地圖中的條目。 更準確地說,我想將隊列鍵存儲到地圖tuple.first(int)中。出於某些功能原因,我無法提前完成此操作。 這則看起來像如下:
QueuePairType currPriorityPair = priorityQueue.top();
priorityQueue.pop();
int currPriority = currPriorityPair.first;
EdgePairType currEdgePair = currPriorityPair.second;
... = currEdgePair.first;
std::tuple<int, std::vector<HlEdgeEntry*>*> currTuple =
currEdgePair.second;
int newKey = recalculateKeyFromQueueForCurrentElement();
if(newKey > currPriority)
{
//reinsert currElement into Queue and go to next iteration
continue;
}
//set currPriority, cannot change any more in future
std::get<int>(currTuple) = newKey;
所以這段代碼剪斷的最後一行顯示了我想做的事情,使得地圖內的值發生了變化。
據我所知,這是不可能的,直到我使用指針作爲地圖內的映射值。但正如我所說,它仍然存在的代碼,我真的會避免改變。 但我的知識不是最好的,所以可能會有一些可能性。
如果沒有,也許這是一種更少的努力?
在此先感謝。
爲什麼不只是有指針的優先級隊列? –
你的代碼有點不清楚。 (什麼是'k'?什麼是'edgesOrelSource'?)我們可以跳過細節,並且可以說優先級隊列是否總是包含已經在地圖中的元素? –
他們的其他對象這裏不感興趣我認爲 「優先級隊列是否總是包含已經在地圖中的元素」 - >是的 – Kaspatoo