首页 > 其他 > 详细

STL自定义比较函数

时间:2015-02-08 23:07:13      阅读:327      评论:0      收藏:0      [点我收藏+]

调用sort函数需要加头文件<algorithm>

sort函数默认的比较函数为(简化)

bool comp(int a,int b){

return a<b;

}

即默认排序为从小到大,如果想从大到小,只需要作如下修改

bool comp(int a,int b){

return a<b;

}

调用sort函数时,显性调用comp函数,sort(begin,end,comp)

 

结构体自定义排序规则

有两种方法,举例说明

在结构体内重载运算符<

struct temp{

int w;

int p;

temp(){

w=0;

p=0;

}

bool operator <(const temp &t)const{

return w<t.w     //按w的大小从小到大排列

}

}

定义比较函数,与前面相同

STL自定义比较函数

原文:http://www.cnblogs.com/celineccoding/p/4280554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!