-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