码迷,mamicode.com
首页 > 其他好文 > 详细

STL自定义比较函数

时间:2015-02-08 23:09:35      阅读:231      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!