删除重复项而不进行四舍五入,C++
我正在尝试使用以下比较器
bool operator() ( const Point * p1, const Point * p2 ) const
{
return ( p1->X() < p2->X() ) || ( ( p1->X() == p2->X() ) && ( p1->Y() < p2-Y() ) );
}
和重载操作器==
bool Point::operator == ( const Point &p ) const
{
return ( x - p.x ) * ( x - p.x ) + ( y - p.y ) * ( y - p.y ) < DIFF;
}
原理图删除从向量中删除重复项:
std::sort ( it_begin, it_end, Comp );
Points::iterator i_new_end = std::unique ( it_begin, it_end, Uniq);
items.erase ( i_new_end, this->items.end() );
但是数据存在问题。根据 x 坐标排序的点
-0.0000000015 -6281103.8487118632 0.0000000000
-0.0000000011 -5993359.5353725236 0.0000000000
-0.0000000010 -5523510.0253371494 0.0000000000
-0.0000000009 -4885831.4582128422 0.0000000000
-0.0000000009 -4099699.3745807474 0.0000000000
-0.0000000008 -3189000.0000000000 0.0000000000
-0.0000000008 -2181404.4741311157 0.0000000000
-0.0000000008 -1107528.0771596823 0.0000000000 //unique
-0.0000000008 -0.0000000005 0.0000000000
-0.0000000007 1107528.0771596811 0.0000000000 //unique
-0.0000000007 2181404.4741311143 0.0000000000
-0.0000000007 3188999.9999999991 0.0000000000
-0.0000000006 4099699.3745807474 0.0000000000
-0.0000000006 4885831.4582128404 0.0000000000
-0.0000000005 5523510.0253371485 0.0000000000
-0.0000000004 5993359.5353725236 0.0000000000
0.0000000000 -6281103.8487118632 0.0000000000
0.0000000004 5993359.5353725236 0.0000000000
0.0000000005 5523510.0253371485 0.0000000000
0.0000000006 4099699.3745807474 0.0000000000
0.0000000006 4885831.4582128404 0.0000000000
0.0000000007 1107528.0771596811 0.0000000000
0.0000000007 2181404.4741311143 0.0000000000
0.0000000007 3188999.9999999991 0.0000000000
0.0000000008 -3189000.0000000000 0.0000000000
0.0000000008 -2181404.4741311157 0.0000000000
0.0000000008 -1107528.0771596823 0.0000000000
0.0000000008 -0.0000000005 0.0000000000
0.0000000009 -4885831.4582128422 0.0000000000
0.0000000009 -4099699.3745807474 0.0000000000
0.0000000010 -5523510.0253371494 0.0000000000
0.0000000011 -5993359.5353725236 0.0000000000
0.0000000015 -6281103.8487118632 0.0000000000
0.0089638987 -6377999.9999999991 0.0000000000
运算符 == 不会带来任何效果,附近的点不会彼此相邻排序...
是否有可能在不进行舍入的情况下删除此类重复点(例如 diffrenet 比较器)?我知道,坐标有很多小数位......
I am trying to remove duplicate items from vector using the following comparator
bool operator() ( const Point * p1, const Point * p2 ) const
{
return ( p1->X() < p2->X() ) || ( ( p1->X() == p2->X() ) && ( p1->Y() < p2-Y() ) );
}
and overloaded opeartor ==
bool Point::operator == ( const Point &p ) const
{
return ( x - p.x ) * ( x - p.x ) + ( y - p.y ) * ( y - p.y ) < DIFF;
}
schematic removing:
std::sort ( it_begin, it_end, Comp );
Points::iterator i_new_end = std::unique ( it_begin, it_end, Uniq);
items.erase ( i_new_end, this->items.end() );
However there is a problem with data. Points sorted according to x coordinate
-0.0000000015 -6281103.8487118632 0.0000000000
-0.0000000011 -5993359.5353725236 0.0000000000
-0.0000000010 -5523510.0253371494 0.0000000000
-0.0000000009 -4885831.4582128422 0.0000000000
-0.0000000009 -4099699.3745807474 0.0000000000
-0.0000000008 -3189000.0000000000 0.0000000000
-0.0000000008 -2181404.4741311157 0.0000000000
-0.0000000008 -1107528.0771596823 0.0000000000 //unique
-0.0000000008 -0.0000000005 0.0000000000
-0.0000000007 1107528.0771596811 0.0000000000 //unique
-0.0000000007 2181404.4741311143 0.0000000000
-0.0000000007 3188999.9999999991 0.0000000000
-0.0000000006 4099699.3745807474 0.0000000000
-0.0000000006 4885831.4582128404 0.0000000000
-0.0000000005 5523510.0253371485 0.0000000000
-0.0000000004 5993359.5353725236 0.0000000000
0.0000000000 -6281103.8487118632 0.0000000000
0.0000000004 5993359.5353725236 0.0000000000
0.0000000005 5523510.0253371485 0.0000000000
0.0000000006 4099699.3745807474 0.0000000000
0.0000000006 4885831.4582128404 0.0000000000
0.0000000007 1107528.0771596811 0.0000000000
0.0000000007 2181404.4741311143 0.0000000000
0.0000000007 3188999.9999999991 0.0000000000
0.0000000008 -3189000.0000000000 0.0000000000
0.0000000008 -2181404.4741311157 0.0000000000
0.0000000008 -1107528.0771596823 0.0000000000
0.0000000008 -0.0000000005 0.0000000000
0.0000000009 -4885831.4582128422 0.0000000000
0.0000000009 -4099699.3745807474 0.0000000000
0.0000000010 -5523510.0253371494 0.0000000000
0.0000000011 -5993359.5353725236 0.0000000000
0.0000000015 -6281103.8487118632 0.0000000000
0.0089638987 -6377999.9999999991 0.0000000000
Operator == does not bring any effect, near points are not sorted next to each other...
Is there any possibility to remove such duplicit points WITHOUT rounding (for exmple a diffrenet comparator)? I know, that coordinates have to many decimal places...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
std::sort 需要运算符 <,而不是运算符 ==
std::sort needs operator <, not operator ==
如果
x
和y
的类型是float或double,则不能相等可靠地完成。比较时请考虑使用
numeric_limits::epsilon()
或numeric_limits::epsilon()
!您的实现中的
DIFF
是否等于numeric_limits::epsilon()
? (其中 T 是数据类型:float 或 double)If the type of
x
andy
is float or double, then equality cannot be done reliably.Consider using
numeric_limits<double>::epsilon()
ornumeric_limits<float>::epsilon()
when comparison!Is
DIFF
in your implementation equal tonumeric_limits<T>::epsilon()
? (where T is the data type: float or double)std::sort
使用运算符<
。std::unique
使用operator==
。在 STL 中,由
operator==
定义的相等和使用operator<
定义的等价之间存在区别:http://drdobbs.com/184401469#1
std::sort
usesoperator<
.std::unique
usesoperator==
.In STL there is a distinction between equality, defined by
operator==
,and equivalence, defined usingoperator<
:http://drdobbs.com/184401469#1
我想你遇到了一个无法解决的问题。标准
排序算法需要一个排序运算符,它定义
严格的顺序。并且没有办法实现它
“模糊”关系。你最好的选择是定义你的
关系与您的 == 相同,但没有
epsilon:(
将 epsilon 扔到任何地方都会导致
关系秩序不再严格,并将导致
排序算法中未定义的行为。)
I think you've encountered an unsolvable problem. The standard
sort algorithm requires an ordering operator which defines
a strict ordering. And there's no way to implement it with
"fuzzy" relationships. You're best bet is to define your
relationship along the same lines as your ==, but without the
epsilon:
(Throwing the epsilon in there anywhere will cause the
relationship order to cease to be strict, and will result in
undefined behavior in the sort algorithm.)