3 ms·
Maybe it’s lack of imagination but I cannot see how qsort is easier to use than std::sort. In particular because std::sort is templated you can use anything ca
by jeeeb 5y ago
Maybe it’s lack of imagination but I cannot see how qsort is easier to use than std::sort.
In particular because std::sort is templated you can use anything callable including a lambda or function pointer as the comparator. But if you already have operator< defined for the type you don’t even need to define a comparator.
It seems like std::sort is shorter and simpler to write, safer, faster and generic across container types (including c arrays).
- jstimpfle 5y agoI _believe_ std::sort used to be more complicated because you had to define a class with a compare method for custom comparison functions, or something like that. Looking here https://en.cppreference.com/w/cpp/algorithm/sort https://en.cppreference.com/w/cpp/algorithm/sort , I cannot be arsed to find out what works since which version of C++. Come on, look at that link and tell me with a straight face you can't grok the qsort() signature much more easily than that mess. Other than that, the STL is generally slower compiling compared to only including plain C headers, which is also taxing on the "ease of use" department.
- dataangel 5y ago`sort(begin(foo), end(foo))` Has worked in C++ for ~25 years as long as `foo` is a container of items with an `operator<`. It has never required defining a separate class. It will also optimize better than qsort when qsort fails to inline.
- slavik81 5y agoYour main point is correct, but std::begin and std::end were added in C++11. You would have used pointers or begin/end member functions for most of those 25 years.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]