这种比较是否不一致(或者还有其他问题)?

Zac*_*ack 1 c++ sorting iterator comparator

下面的代码在我的 mac 上出现段错误;但是,在 Linux 上工作正常(甚至没有 valgrind 错误)。

我怀疑比较函数给出了不一致的结果;但是,我看不到如何。

(我有一种感觉,当有人指出时,我会觉得自己很愚蠢:)

上下文:这是学生的代码。我知道有更好的编码方法,我只是不知道为什么它是错误的。

using namespace std;

using Point = std::pair<double, double>;
using PointVector = vector<Point>;
extern PointVector cluster1;

bool sortComparison(const Point &point1, const Point &point2) {

  if(point1.first < point2.first)
    return true;
  else if(point1.first > point2.first)
    return false;
  else if(point1.second < point2.second)
    return true;
  else if(point1.second > point2.second)
    return false;
  else
    return true;
}


int main(int argc, char *argv[]) {

  cout << "In" << endl;
  std::sort(cluster1.begin(), cluster1.end(), sortComparison);
  cout << "Out" << endl;

}
Run Code Online (Sandbox Code Playgroud)

Fir*_*cer 5

std::sort如果第一个参数小于第二个 ( a < b),则比较函数应返回 true ,但如果相等 ( a <= b,由于),则返回 true else return true;。这可能会破坏std::sort.