C++:STL中sort()函数的用法总结

借鉴:
http://blog.csdn.net/appte/article/details/8253930。

http://blog.csdn.net/csust_acm/article/details/7326418。

在做ACM题的过程中,算法中经常会用到排序的处理,自己写排序有点麻烦,最主要是记不住排序算法的代码。。。 STL中sort()函数的用法,所以就取巧适用STL中的sort函数,这个省心又放心,只需要包含头文件就可以了,这里总结一下用法以作备忘。
STL中的sort函数有两种:
template
void sort(RanIt fist, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt fist, RanIt last, Pred pr);
1.默认的sort函数是按升序排。

  sort(a,a+n);    //两个参数分别为待排序数组的首地址和尾地址

2.可以自己写一个cmp函数,按特定意图进行排序,例如结构体,这个是比较好用的。
例如:

int cmp( const int &a, const int &b){
if( a > b )
return1;
else
return0;
}
sort(a,a+n,cmp);//是对数组a降序排序
也可以对结构体进行排序。

例如杭电oj里1009题可以用结构体来解决:

//hdoj_1009 贪心,输入输出格式可能不对
#include
#include
#include <stdio.h>
#define MAX 1000
using namespace std;
struct Node
{
double jValue;
double fValue;
};

int cmp(const Node a,const Node b)
{
if(b.fValue!=0) {
return a.jValue/a.fValue > b.jValue/b.fValue;//不知道怎么回事这个地方用return 1,0,-1的方式排序不成功
}
else return 0;
}

int main()
{
Node list[MAX];
int i;
for(i=0;i<MAX;i++) {
list[i].jValue=0;
list[i].fValue=0;
}

int m,n;
scanf("%d %d",&m,&n);
while(m != -1 && n != -1) {
    printf("m is:%d,n is:%d\n",m,n);
    int a,b;
    for(i=0;i<n;i++) {
        scanf("%d %d",&a,&b);
        list[i].jValue=a;
        list[i].fValue=b;
    }
    sort(list,list+n,cmp);
   
    double sum=0.0;
    for(i=0;m>0&&i<n;i++)
    {
        if(m>list[i].fValue) {
            m -= list[i].fValue;
            sum += list[i].jValue;
        }
        else {
            sum += (m*list[i].jValue/list[i].fValue);
            break;//break一定不能漏了。因为m没减掉0,会再进来一次
        }
    }
    printf("sum is:%.3lf\n",sum);
    scanf("%d %d",&m,&n);      
}

getchar();
getchar();
return 0;

}
其实还有qsort的排序库函数,不过这个与STL中的sort函数使用方法不大相同,因为这里的qsort参数均为void类型所以要转一下,自定义的比较函数也会有不同,不同的数据类型也不一样,例如:

一、对int类型数组排序

int num[100];
int cmp ( const void *a , const void *b )
{
return (int)a - *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);

二、对char类型数组排序(同int类型)
char word[100];

int cmp( const void *a , const void b )
{
return
(char *)a - *(int *)b;
}
qsort(word,100,sizeof(word[0]),cmp);
三、对double类型数组排序(特别要注意)
double in[100];
int cmp( const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
}
qsort(in,100,sizeof(in[0]),cmp);
四、对结构体一级排序
struct In
{
double data;
int other;
}s[100];
//按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种,参考上面的例子写
int cmp( const void *a ,const void *b)
{
return ((In *)a)->data - ((In *)b)->data ;
}
qsort(s,100,sizeof(s[0]),cmp);
五、对结构体
struct In
{
int x;
int y;
}s[100];
//按照x从小到大排序,当x相等时按照y从大到小排序
int cmp( const void *a , const void *b )
{
struct In *c = (In *)a;
struct In *d = (In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}
qsort(s,100,sizeof(s[0]),cmp);

六、对字符串进行排序
struct In
{
int data;
char str[100];
}s[100];

//按照结构体中字符串str的字典顺序排序
int cmp ( const void *a , const void *b )
{
return strcmp( ((In *)a)->str , ((In *)b)->str );
}
qsort(s,100,sizeof(s[0]),cmp);

七、计算几何中求凸包的cmp
int cmp(const void *a,const void *b) //重点cmp函数,把除了1点外的所有点,旋转角度排序
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if( calc(*c,*d,p[1]) < 0)
return 1;
else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y)) // 如果在一条直线上,则把远的放在前面
return 1;
else return -1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值