背景:我正在寫一個算法,它將對象的地圖存儲到我想積累的關聯屬性。這是一個級聯分配程序,它使用通過網絡的預定義路徑將此屬性加載到網絡中。路徑被定義爲從網絡中的原點到網絡中的所有點的構建轉發路徑。自定義嚴格弱訂購
問:要做到這一點我在級聯方法使用地圖自定義比較
bool pathLinkComp(const PathLink* lhs, const PathLink* rhs)
{
return (lhs != rhs) && (lhs->cost < rhs->cost);
}
然後我以下列方式使用
PathLinkTripsMap myMap(pathLinkComp);
myMap[pathLinkOfInterest] = 100.0;
// populate other pathLinksOfInterest with initial values
while (myMap.size())
{
// pop
auto firstIterator = myMap.end(); --firstIterator;
PathLink* link = firstIterator->first;
double trips = firstIterator->second;
myMap.erase(firstIterator);
// do something with the popped data
// move the trips back onto the previous link (unless we are at the end of the path)
PathLink* backLink = link->backLink;
if (backLink) myMap[backLink] += trips;
}
這樣做的問題是,如果我使用嚴格的弱排序,然後結束這種情況,如果兩個PathLink對象具有完全相同的成本,那麼它們實際上成爲用於索引目的的相同對象。如果,而不是<,我使用< =我得到正確的行爲,但顯然這並沒有給出一個嚴格的弱排序,這是std :: map的比較器應該做的事情......這是一件大事,迫使std :: map以這種方式運行?
或者,我怎樣才能構建我的比較器來實現嚴格的弱點和保持單獨的鍵分離?
語言故障在那裏......「如果我使用<=而不是<,那麼....」... <=不會給strick-weak-ordering – 2011-02-06 22:46:46
這應該(至少)是'(lhs - >費用< rhs->費用)|| ((lhs-> cost == rhs-> cost)&& std :: less()(lhs,rhs))`。 –
2011-02-06 22:48:04