Sort (C++) - Comparison To Qsort

Comparison To Qsort

sort can be compared to the qsort function in the C standard library (in stdlib.h). sort is templated, so it uses the correct comparison function for whatever data type you are sorting, which is already implemented for standard data types. Otherwise you can specify your own comparison function, which can be type-checked by the compiler; whereas for qsort, you must manually cast pointers to the desired type in an unsafe way. Also, qsort accesses the comparison function using a function pointer, necessitating large numbers of repeated function calls, which can take a lot of time; whereas in C++, comparison functions are usually inlined, removing the need for function calls. In practice, C++ code using sort is often many times faster at sorting simple data like integers than equivalent C code using qsort.

Read more about this topic:  Sort (C++)

Famous quotes containing the words comparison to and/or comparison:

    It is very important not to become hard. The artist must always have one skin too few in comparison to other people, so you feel the slightest wind.
    Shusha Guppy (b. 1938)

    What is man in nature? A nothing in comparison with the infinite, an all in comparison with the nothing—a mean between nothing and everything.
    Blaise Pascal (1623–1662)