删除重复项而不进行四舍五入,C++

发布于 2024-10-22 01:37:55 字数 2532 浏览 1 评论 0原文

我正在尝试使用以下比较器

            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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一腔孤↑勇 2024-10-29 01:37:56

std::sort 需要运算符 <,而不是运算符 ==

std::sort needs operator <, not operator ==

迷爱 2024-10-29 01:37:56

运算符==不会带来任何效果,近点不会彼此相邻排序...

如果xy的类型是float或double,则不能相等可靠地完成。

比较时请考虑使用 numeric_limits::epsilon()numeric_limits::epsilon()

您的实现中的 DIFF 是否等于 numeric_limits::epsilon()? (其中 T 是数据类型:float 或 double)

Operator == does not bring any effect, near points are not sorted next to each other...

If the type of x and y is float or double, then equality cannot be done reliably.

Consider using numeric_limits<double>::epsilon() or numeric_limits<float>::epsilon() when comparison!

Is DIFF in your implementation equal to numeric_limits<T>::epsilon()? (where T is the data type: float or double)

离笑几人歌 2024-10-29 01:37:56

std::sort 使用运算符<std::unique 使用operator==

在 STL 中,由 operator== 定义的相等和使用 operator< 定义的等价之间存在区别:

两个对象相等,如果两者都不相等
以某种排序顺序先于另一个
的兴趣。通常,等效值
是平等的,但并不总是平等的。为了
例如,字符串“STL”和“stl”
在不区分大小写的情况下是等效的
排序,但它们肯定不是
平等的。有关区别的详细信息
在等价与平等之间,
查阅有关 STL 的任何好的参考资料。
在Effective STL中,问题是
在第 19 项中进行了检查。

http://drdobbs.com/184401469#1

std::sort uses operator<. std::unique uses operator==.

In STL there is a distinction between equality, defined by operator==,and equivalence, defined using operator<:

Two objects are equivalent if neither
precedes the other in some sort order
of interest. Often, equivalent values
are equal, but not always. For
example, the strings “STL” and “stl”
are equivalent in a case-insensitive
sort, but they are certainly not
equal. For details on the distinction
between equivalence and equality,
consult any good reference on the STL.
In Effective STL, the issue is
examined in Item 19.

http://drdobbs.com/184401469#1

自此以后,行同陌路 2024-10-29 01:37:56

我想你遇到了一个无法解决的问题。标准
排序算法需要一个排序运算符,它定义
严格的顺序。并且没有办法实现它
“模糊”关系。你最好的选择是定义你的
关系与您的 == 相同,但没有
epsilon:(

inline double mag2( Point const& p )
{
    return p.X() * p.X() + p.Y() * p.Y();
}

bool operator()( Point const* lhs, Point const* rhs )
{
    return mag2( *lhs ) < mag2( *rhs );
}

将 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:

inline double mag2( Point const& p )
{
    return p.X() * p.X() + p.Y() * p.Y();
}

bool operator()( Point const* lhs, Point const* rhs )
{
    return mag2( *lhs ) < mag2( *rhs );
}

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文