c++ - std::vector 和 algorithm::sort ,以下代码有什么问题

标签 c++ stl vector

我正在尝试排序 std::vector使用 algorithm::sort , 但我收到运行时错误 Invalid operator < .

以下是我的代码。

struct Point {
    double x_cord;
    double y_cord;
    int id;
    Point(int d, double x, double y) {
        x_cord = x;
        y_cord = y;
        id = d;
    }
};

struct compareX {
    bool operator ()(Point * left, Point* right) const {
        if (left->x_cord < right->x_cord) 
            return true;
        return true;
    }
};
struct compareY {
    bool operator ()(Point * left, Point* right) const {
        if (left->y_cord <= right->y_cord) return true;
        return true;
    }
};

现在我在填充值后调用它。

std::sort( posVector.begin(), posVector.end(), compareX());

最佳答案

你的比较函数总是返回真!

关于c++ - std::vector 和 algorithm::sort ,以下代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6257166/

相关文章:

c++ - (Visual C++ v141) 按位函数编译不正确

c++ - FPGA开发入门

c++ - c++ 中的 pair class STL 有什么用,它像数据类型还是容器?

math - 混合两个 RGB 颜色向量以获得结果

R 变量中的变长向量或列表

c# - 为什么 C# PInvoke 不能与非托管 DirectX 一起工作

c++ - 在头文件中使用 std

c++ - 设置运行时刻bitset的大小

c++ - 讨论了哪些 boost 库包含在 C++17 中?

c++ - 使用迭代器在 vector C++ 中循环