Comment

Comment by ot

Another common class of bugs in C++ is comparison functions that don't respect the strict weak ordering axioms [1]. I've seen a lot of implementations in which cmp(x, x) is true.

This causes undefined behavior in STL algorithms, for example std::sort can easily go out of bounds.

A good rule of thumb to remember the axioms is that the comparator should behave like <, not <=.

[1] http://en.cppreference.com/w/cpp/concept/LessThanComparable

parent ot 8 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton