【发布时间】:2016-01-20 22:12:39
【问题描述】:
我正在使用 C++ 集容器来删除重复项,但我遇到了用户定义类的问题。我将比较运算符定义如下,效果很好:
bool operator < (const TLineSegment& other) const
{
int cx1 = (int)((x1 + x2) / 2.0);
int cy1 = (int)((y1 + y2) / 2.0);
int cx2 = (int)((other.x1 + other.x2) / 2.0);
int cy2 = (int)((other.y1 + other.y2) / 2.0);
if (cx1 == cx2)
return (cy1 < cy2);
else
return (cx1 < cx2);
}
但是,下面的代码通过添加更多的条件(在一定范围内没有重复)包含很少的重复。我发现它在大多数情况下都有效,但有时它不起作用,真的很奇怪。我发现的一种情况是两个 TLineSegments 是相同的(但不是同一个实例)并且它们都被插入了。
bool operator < (const TLineSegment& other) const
{
const int epi = 5;
int cx1 = (int)((x1 + x2) / 2.0);
int cy1 = (int)((y1 + y2) / 2.0);
int cx2 = (int)((other.x1 + other.x2) / 2.0);
int cy2 = (int)((other.y1 + other.y2) / 2.0);
if (abs(cx1 - cx2) < epi) {
if (abs(cy1 - cy2) < epi) {
return false;
}
else {
return (cy1 < cy2);
}
}
else {
return (cx1 < cx2);
}
【问题讨论】:
-
结果相等呢?你似乎没有处理这种情况。
-
您的逻辑有缺陷(x 轴上的所有线,其中 x1 = -n 和 x2 = +n,对于任何 n 都相同)
-
您是有意只比较中心还是线段?两个比较器都会说中心相同的两条不同的线段相等。
-
std::set 复制对象,因为它假定
标签: c++ insert set duplicates