Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > Why use sort() when we have "good old qsort()"?
【标  题】:Why use sort() when we have "good old qsort()"?
【关键字】:Why,use,sort,when,we,have,good,old,qsort
【来  源】:http://blog.csdn.net/steven216/archive/2007/02/23/1512862.aspx

Why use sort() when we have "good old qsort()"?

Your Ad Here 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 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.  

避免返回内部数据的句柄:【上一篇】
Eclipse快捷键-方便查找,呵呵,记性不好:【下一篇】
【相关文章】
  • 基于WEB分布式信息系统的设计
  • ESB在实际项目重的应用--WebSphere Message Broker
  • 如何让.NET中的强类型的排序列表SortedList支持重复键
  • Ultimate Web 设计可用性检查列表
  • Hack htmlize.el for "src" tag of Emacs Muse
  • welcome to linux from scratch
  • Web服务已不再Cool
  • LFS6.1.1构建GNU-i686工具链笔记(why to)
  • 如何对 IIS 5.0 禁用 WebDAV
  • 用NetUser命令设置用户密码永不过期(帮助勾选)
  • 【随机文章】
  • 三度空间:微软P2P工具全攻略之使用篇
  • 使用数据库建模工具进行数据库建模
  • Nero StartSmart 6.6.0.16
  • QuickCD 1.0.320破解手记--算法分析(2)
  • MYSQL客户机程序3—产生连接代码模块
  • dhTabStrip 1.0发布
  • [Share]《JSP基础》讲义
  • 个人轻松组建宽带局域网
  • 此时+彼时的郁闷
  • 转 TCP/IP详解学习笔记(11)-TCP交互数据流,成块数据流
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.