C++sort函数导致crash; Expression: invalid comparator

洪子晋
2023-12-01

今天程序崩溃了,提示也很明显

Debug Assertion Failed!

Program: ...esktop_Qt_5_12_0_MSVC2017_32bit-Debug\debug\superCoaching.exe
File: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xutility
Line: 1113

Expression: invalid comparator

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

主要就是比较函数导致的问题,

网上查阅得知比较函数必须按照严格弱序排序,也就是严格定义<,a和b比较,如果a==b,必须返回false

https://stackoverflow.com/questions/9648100/using-own-comparator-operator-for-map-giving-error-in-case-if-key-not-found

重点是The comparison operator must define a strict weak ordering,然后查一下strict weak ordering就明朗了

 类似资料: