每日学习笔记:C++ STL算法之容器元素排序

目录

常规排序

升序排序:

sort(beg, end)

stable_sort(beg, end, op)

自定义规则排序:

sort(beg, end, op)

stable_sort(beg, end, op)

局部排序(使前段有序)

partial_sort(beg, sortEnd, end)

partial_sort(beg, sortEnd, end, op)

复制并局部排序

partial_sort_copy(srcBeg, srcEnd, destBeg, destEnd)

partial_sort_copy(srcBeg, srcEnd, destBeg, destEnd, op)

令第n个元素处在升序序列状态的正确位置上(可用于查找前N个最大值或最小值)

nth_element(beg, nth, end)

nth_element(beg, nth, end, op)

按Heap(堆)排序

heap介绍

将区间元素顺序转换为堆的顺序:

make_heap(beg, end)

make_heap(beg, end, op)

将区间最后一个元素加入原来的堆区间内,并以堆排序:

push_heap(beg, end)

push_heap(beg, end, op)

将原来堆的最高元素移到区间最后,并重新将其余元素以堆排序:

pop_heap(beg, end)

pop_heap(beg, end, op)

将区间堆顺序转换回sort()排序序列:

sort_heap(beg, end)

sort_heap(beg, end,op)


常规排序

升序排序:

sort(beg, end)

stable_sort(beg, end, op)

自定义规则排序:

sort(beg, end, op)

stable_sort(beg, end, op)

局部排序(使前段有序)

partial_sort(beg, sortEnd, end)

partial_sort(beg, sortEnd, end, op)

复制并局部排序

partial_sort_copy(srcBeg, srcEnd, destBeg, destEnd)

partial_sort_copy(srcBeg, srcEnd, destBeg, destEnd, op)

令第n个元素处在升序序列状态的正确位置上(可用于查找前N个最大值或最小值)

nth_element(beg, nth, end)

nth_element(beg, nth, end, op)

按Heap(堆)排序

heap介绍

只适用于支持随机访问迭代器的容器

例如:容器内容如下:

下图标出容器下标如下:

将区间元素顺序转换为堆的顺序:

make_heap(beg, end)

make_heap(beg, end, op)

将区间最后一个元素加入原来的堆区间内,并以堆排序:

push_heap(beg, end)

push_heap(beg, end, op)

将原来堆的最高元素移到区间最后,并重新将其余元素以堆排序:

pop_heap(beg, end)

pop_heap(beg, end, op)

将区间堆顺序转换回sort()排序序列:

sort_heap(beg, end)

sort_heap(beg, end,op)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值