使用自定义函数的 C++ std::sort

标签 c++ sorting boost stl std

我有一个结构点:

typedef struct Point
{
    double x;
    double y;

    Point operator-(const Point &b)
    {
        Point pt;
        pt.x = this->x - b.x;
        pt.y = this->y - b.y;
        return pt;
    }

    friend std::ostream& operator<<(std::ostream& os, const Point& pt)
    {
        return os << "X: " << pt.x << "\t\t\tY: " <<pt.y << std::endl; 
    }

    friend std::istream& operator>>(std::istream& is, Point& pt)
    {
        return is >> pt.x >> pt.y;
    }
}Point;

我试图从 vector<Point> 中找到最小元素然后根据与该最小点的角度进行排序。

下面是相应的代码:

bool lex_yx(const Point &a, const Point &b)
{
    if(a.y < b.y)
        return true;

    if (a.y == b.y)
        return (a.x < b.x);

    return false;
}

bool CalcAngRad_Compare (const Point &p_min, Point &a, Point &b)
{
    Point subPt_1, subPt_2;
    double a_r, b_r, a_angle, b_angle;

    subPt_1 = a - p_min, subPt_2 = b - p_min;

    a_angle = atan2(subPt_1.y, subPt_1.x);
    b_angle = atan2(subPt_2.y, subPt_2.x);

    if (a_angle < b_angle) {return true;}
    if (a_angle > b_angle) {return false;}

    a_r = subPt_1.x * subPt_1.x * subPt_1.y * subPt_1.y; 
    b_r = subPt_2.x * subPt_2.x * subPt_2.y * subPt_2.y; 

    return (a_r < b_r); // return (a_r <= b_r); // Code crashes, saying invalid operator <. I do not know why. Pl tell me.
}

auto it  = std::min_element(V.begin(), V.end(), lex_yx);
std::sort(  V.begin(), V.end(), boost::bind(CalcAngRad_Compare, *it, _1, _2)); 

当我传递一个简单的测试输入,例如

2 2
3 3
4 4
1 1 // this should be first element in the sorted array
5 5

它有效,我得到了排序后的数组。但现在看一下另一个简单的输入:

-0.5    -0.1
-0.1    -0.1
0       1
-1      -0.1 // this should be first element is sorted array. BUT IT IS NOT!!
5       5

我不认为我做错了什么。一个问题可能是当 y 坐标相等时,角度为 0 或 pi。在这种情况下,如果半径也相等,则 I return false 。我尝试将其更改为 return (a_r <= b_r) ,但这似乎不可能。代码崩溃,提示无效运算符<。 (在文件 xutility 中,这一行为 true: else if (_Pred(_Right, _Left)) 。我不明白正在测试什么,可能检查了一些优先级。)

我希望你回答的是:

  1. 如何解决问题并始终获得(正确)排序的 vector
  2. 还有其他方法可以实现我正在做的事情吗?
  3. 我非常有兴趣了解我的思维/实现风格存在什么问题?

最佳答案

How can I solve the issue and always get the (correctly) sorted vector Is there any other way of achieving what I am doing?

不确定您想要的顺序,less_yx 可以吗?

I am very much interested in learning what is problem in my thinking/implementation style?

假设 IEEE 浮点运算(否则 0, 0 会出现域错误)

由于您的引用点是最低的 y,因此传递给 atan2 的第二个参数为正值或 null。

- `atan2(0, 0) = 0`
- `atan2(+x, 0) = 0`
- `atan2(x, +y) > 0`

因此,根据 atan2,任何最低的 y 都是等效的。

a_r = subPt_1.x * subPt_1.x * subPt_1.y * subPt_1.y;
对于 subPt_1.y == 0

始终为 0。 你的意思是

a_r = subPt_1.x * subPt_1.x + subPt_1.y * subPt_1.y;

相反? (因此即使 y == 0 也会比较 x)

关于使用自定义函数的 C++ std::sort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34575015/

相关文章:

c++ - CreateRemoteThread 因 ERROR_NOT_ENOUGH_MEMORY 而失败

c++ - 模型减慢游戏速度 - opengl

java - 从单词数组中搜索单词中的子单词?

python - 获取列表升序的索引

c++ - 数据集(和样本)何时在 boost::test 中破坏?

c++ - boost/property_tree/xml_parser.hpp : No such file or directory

C++ STL 低级编程

C++(在 Linux 下)程序没有给出预期的输出(定时器)

java - 随机错误 - java.lang.IllegalArgumentException : Comparison method violates its general contract

c++ - FREENECT_DEPTH_REGISTERED 对 libfreenect 没有影响