Why use sort() when we have "good old qsort()"?
2007-02-23 08:21
387 查看
To a novice,
looks pretty weird, and is harder to understand than
To an expert, the fact that sort() tends to be faster than qsort() for the same elements and the same comparison criteria is often significant. Also, sort() is generic, so that it can be used for any reasonable combination of container type, element type, and comparison criterion. For example:
In addition, most people appreciate that sort() is type safe, that no casts are required to use it, and that they don't have to write a compare() function for standard types.
For a more detailed explanation, see my paper "Learning C++ as a New language", which you can download from my publications list.
The primary reason that sort() tends to outperform qsort() is that the comparison inlines better.
qsort(array,asize,sizeof(elem),elem_compare);
looks pretty weird, and is harder to understand than
sort(vec.begin(),vec.end());
To an expert, the fact that sort() tends to be faster than qsort() for the same elements and the same comparison criteria is often significant. Also, sort() is generic, so that it can be used for any reasonable combination of container type, element type, and comparison criterion. For example:
struct Record { string name; // ... }; struct name_compare { // compare Records using "name" as the key bool operator()(const Record& a, const Record& b) const { return a.name<b.name; } }; void f(vector<Record>& vs) { sort(vs.begin(), vs.end(), name_compare()); // ... }
In addition, most people appreciate that sort() is type safe, that no casts are required to use it, and that they don't have to write a compare() function for standard types.
For a more detailed explanation, see my paper "Learning C++ as a New language", which you can download from my publications list.
The primary reason that sort() tends to outperform qsort() is that the comparison inlines better.
相关文章推荐
- When and Why do we use "#if 0"
- what is the virtual machine, when and why we need use it ?
- Why we use extern "C"
- what is WLAN ? when and why we need use it ?
- Why we have to use epsg:900913 in OpenLayers
- How do we release the GDI object when we use selectobject choosing a brush?
- why we use abstract_class in active_record ?
- When to use method="get"?
- how to fix "Cannot find a valid baseurl for repo: poptop-stable/7" in centos 7 *64 when use "yum -y
- ASP.NET features we use, ASP.NET experience I have
- What does AspCompat="true" mean and when should I use it?
- When we use PdfStamper to merge fields.
- When should we use PUT and when should we use POST?
- Why we should use closures in python
- [转] When to use what language and why
- How do I fix "selector not recognized" runtime exceptions when trying to use category methods from a static library?
- 148. Everythins is good when new, but friends when old. 东西是新的好,朋友是老的亲
- Why do we get error "Call to undefined function: ora_logon()/ ocilogon()"?
- What does AspCompat="true" mean and when should I use it?
- The XML Litmus Test Understanding When and Why to Use XML