By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

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

P: n/a
hello,
can someone tell me
what is the difference between sort() (in stl) & qsort(stdlib.h)
thanks in advance.

Oct 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<du*********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Oct 22 '05 #2

P: n/a

du*********@gmail.com wrote:
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

Oct 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.