what is the difference between sort() (in stl) & qsort(stdlib.h)

D

duyifan.nju

hello,
can someone tell me
what is the difference between sort() (in stl) & qsort(stdlib.h)


thanks in advance.
 
M

Mike Wahler

hello,
can someone tell me
what is the difference between sort() (in stl) & qsort(stdlib.h)

The essential differences:

std::sort uses iterators (which include pointers),
whereas qsort uses pointers.

std::sort has a default comparison operator (< operator),
whereas qsort requires a user-supplied comparison function.

std::sort can be used to sort standard containers (except
std::list, which has its own member sort function) or arrays,
qsort cannot sort standard containers, but only arrays.


-Mike
 
G

Greg

hello,
can someone tell me
what is the difference between sort() (in stl) & qsort(stdlib.h)


thanks in advance.

The most important different is that std::sort() is typesafe whereas
qsort() is not. In other words, when using qsort() a program is given a
pair of void * pointers to rank in its comparison routine; with
std::sort() the program is given two "pointers" (technically,
iterators) of a specific type and asked to rank them. Preserving the
type information in the comparison routine makes mix-ups less likely
when using std::sort(), and the comparison routines themselves are much
easier to match with the type that each knows how to rank.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top