Why use sort() when we have "good old qsort()"?
来源:互联网 发布:win10看不到mac分区 编辑:程序博客网 时间:2024/05/17 00:55
To a novice,
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 keybool 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.
- Why use sort() when we have "good old qsort()"?
- When and Why do we use "#if 0"
- Why don't we have virtual constructors?
- ios7下 attempting to set a swipe to delete cell when we already have one.... that doesn't seem good
- why we use abstract_class in active_record ?
- Why we should use closures in python
- why we use Symbols in Hash
- Why Do We Use Fiber Optic Switch?
- when can we use machine learning ?
- When should we use PUT and when should we use POST?
- why should we use requset_mem_region()before using ioremap()?
- 我们为什么用卷积? Why should we use convolution?
- Why we should use ENUM instead of String ?
- Why do we use a Least Squares fit?
- Why do we use int rather than char
- Why do we use n = 16 * n + hexdigit
- String intern() method in Java. Why we use it?
- 【Zookeeper】——Why should we use zookeeper?
- 据说是熊猫烧香源代码
- Universal Extract 1.5
- 视频网站收集
- 计算机科学经典论文
- SPS体会
- Why use sort() when we have "good old qsort()"?
- 避免返回内部数据的句柄
- 一些找工作的网址
- 昨天写了VBA
- VB.NET中的回车换行
- Hanoi
- 设置纸张边距
- CSV出力
- 飞燕惊龙主题曲歌词怀旧..