std::sort(v.begin(), v.end(), [](int a, int b){ return a >= b; }); is called. What is the defect?
Correct answer
C. The predicate is not a strict weak ordering, so the sort behaviour is undefined
15 views
C. The predicate is not a strict weak ordering, so the sort behaviour is undefined
15 views
Strict weak ordering requires irreflexivity, so comp(x, x) must be false; with a >= b the algorithm may read out of bounds, and the standard declares the behaviour undefined. Descending order should be written a > b. Debug builds of libstdc++ with _GLIBCXX_DEBUG often catch this as an assertion about the comparator, while release builds may return a silently wrong order.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.