-3
後從預期值不同基本上我有地圖大小添加值到地圖
map<pair<int, float>, int>
類型的edgeCostMap和42點的頂點的向量。我通過頂點的矢量循環,如下增加值到地圖:環已完成
後for(int vertexIndex = 0; vertexIndex < V.size(); vertexIndex++)
{
pair<int, float> toAdd;
toAdd.first = vertexIndex;
toAdd.second = V[vertexIndex].edgeCollapseCost;
edgeCostMap[toAdd] = vertexIndex;
}
不過,我打印出圖內容如下:
for(map<pair<int, float> ,int>::iterator it = edgeCostMap.begin(); it != edgeCostMap.end(); it++)
{
logFile<<"Vertex "<<it->second<<" has cost "<<it->first.second<<" has "<<mapVF[it->second].size()<<"neighbors"<<endl;
}
我只得到回12條陳述。我的貼圖是不正確的?
比較功能:
class comparator {
public:
bool operator()(const std::pair<int, float>& a, const std::pair<int, float>& b) const {
return a.second < b.second;
}
};
映射鍵不能被複制,也許std :: multimap是你需要的.http://www.cplusplus.com/reference/map/multimap/ – 2015-03-02 07:55:54
從我所瞭解的我的鍵值是目前的一對其中第一個該對中的元素將引用不同的頂點。重複在哪裏? 我需要一個數據結構,可以保留一堆對,可以維護按浮點值排序的對列表。我不確定multimap是否適合這種情況。 –
jing
2015-03-02 08:07:05
std ::對默認情況下按第一個元素排序,然後是第二個排序,因此爲了使您的地圖按您希望的方式排序,您必須在定義它時提供自定義比較函數。此外,從您提供的代碼中,Ron Tang的答案看起來像是最可能的原因,因此向地圖添加元素時,請檢查鍵(對)是否確實是唯一的。 – Ionut 2015-03-02 08:16:24