首页 > 解决方案 > 为什么我的那套对包含重复项?

问题描述

我对一组包含不一致重复项的 pair<int,int> 感到困惑。集合的第一个元素被正确插入,然后,在插入第 k 对之后,集合中的所有对都被替换为新的 int 值的重复对。

该集合在循环中填充如下

//the coordinates are stored in vector of pair<double,double> with dimension N :
vector<pair<double,double> > coordPairs(N);  

//populate "coordPairs" 

... 


std::set <std::pair<int,int> > occupiedCellList;

for(int i=0; i<coordPairs.size(); i++)
{   
    double x, y;
    x = coordPairs[i].first;
    y = coordPairs[i].second;

    int row = (y - ymin) / cellSizeInMeters;
    int col = (x - xmin) / cellSizeInMeters;
  
    occupiedCellList.insert(make_pair(row,col));              
}
  

即使我在 row 和 col 的表达式中使用 floor 或 trunc ,该集合仍然包含重复项。如何解释这种行为?

谢谢。

标签: duplicatesintegerset

解决方案


我解决了我的问题。

似乎是重复的不是。“错误”来自 QT 创建器中的调试器,它错误地显示了集合元素。

当使用集合迭代器显示集合的 std::cout 元素时,所有元素都正常。

set<pair<int,int>> s;
...
//populate elements of s
...    
set<pair<int,int>>::iterator it = s.begin();
while(it!=s.end())
{
    cout<< it->first << ", " << it->second << endl;
    it++;
}

推荐阅读