sort函数

sort:

包含头文件 algorithm
默认排序方式为升序,但可根据自定义comp函数来达到其他要求。
1、用法
数组:

int a[100];
sort(a, a+100);

vector:

vector<int> v;
sort(v.begin(), v.end());

2、降序排序

int a[100];
bool comp(const int a,const int b)
{
    return a>b;
}
sort(a, a+100, comp);

3、对结构体内某元素排序

struct Node{
    int x;
    int y;
}node[100];
bool comp(const Node a,const Node b){
    return a.a < b.a;
}
sort(node, node+100, comp);

4、对pair排序

pair<int, int> p[100];
bool comp(const pair<int, int> &a, const pair<int, int> &b){
    return a.first < b.first;
}
sort(p, p+100, comp);

不进行typedef定义pair,comp参数里要用pair< int, int >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值