且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

排序功能C ++分段错误

更新时间:2023-12-06 15:58:04

这可能与您的细分错误无关,但是...

This may be unrelated to your segmentation fault, but...

在C ++中,您的比较"谓词必须为严格弱排序 .特别是,"compare(X,X)" 必须为任何X返回"false".在比较函数中,如果两对相同,则击中测试(p1.first <= p2.first),并返回"true".因此,此比较"谓词没有施加严格的弱排序,并且将其传递给排序"的结果是不确定的.

In C++, your "compare" predicate must be a strict weak ordering. In particular, "compare(X,X)" must return "false" for any X. In your compare function, if both pairs are identical, you hit the test (p1.first <= p2.first) , and return "true". Therefore, this "compare" predicate does not impose a strict weak ordering, and the result of passing it to "sort" is undefined.