做leetcode前复习

1、sort

基本用法:sort(vec.beign(), vec.end()); 升序排列。

sort会根据剩余数据的大小自动选择排序策略,无论什么情况,sort都能保证O(N*log2(N)),基本是比你手写的要好的。

高级用法:自定义排序规则。

public:
        static bool cmp(node a,node b)
        {
            if(a.unit!=b.unit)
                return a.unit>b.unit;
            else  
                return a.box>b.box;
        }
public:
		int main()
		{
			sort(vec.begin(), vec.end(), cmp); //这里的vec是node数组
		}
// 当必须要用到main内部其他数据来排序时,就无法用上一种Public定义cmp了。可以用lambda函数。
sort(order.begin(), order.end(), [&](int a, int b){
            if(ages[a] < ages[b])   return true;
            else if(ages[a] == ages[b])
            {
                if(scores[a] < scores[b])   return true;
                else    return false;
            }
            else    return false;
        });

2、

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值