首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::sort_heap

Defined in header <algorithm>

?

?

template< class RandomIt > void sort_heap( RandomIt first, RandomIt last );

(1)

?

template< class RandomIt, class Compare > void sort_heap( RandomIt first, RandomIt last, Compare comp );

(2)

?

转换最大堆[first, last)按升序排列的范围。结果范围不再具有堆属性。

函数的第一个版本使用operator<为了比较元素,第二个函数使用给定的比较函数。comp...

参数

first, last

-

the range of elements to sort

comp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ?true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. ?

类型要求

---。

-取消引用的随机数的类型必须符合可移动分配和移动可建的要求。

返回值

%280%29

复杂性

顶多N×log%28N%29比较N=std::distance(first, last)...

注记

最大堆是一系列元素[f,l)具有以下属性:

  • 带着N = l - f,为所有人0 < i < N,,,f[floor(i-12)]比不上f[i]...
  • 可以使用std::push_heap()
  • 第一个元素可以使用std::pop_heap()

可能的实施

第一版

*。

模板<class Randomit>空排序[医]堆%28 Randomit First,Randomit持续%29;{而%281%21=最后%29 std::POP[医]堆%281,最后-%29;}

第二版

模板<类随机,类比较>空排序[医]堆%28 Randomit First,Randomit Lest,比较Comp%29;{而%281%21=最后%29 std::POP[医]堆%285,最后-,comp%29;}

二次

代码语言:javascript
复制
#include <algorithm>
#include <vector>
#include <iostream>
 
int main()
{
    std::vector<int> v = {3, 1, 4, 1, 5, 9}; 
 
    std::make_heap(v.begin(), v.end());
 
    std::cout << "heap:\t";
    for (const auto &i : v) {
        std::cout << i << ' ';
    }   
 
    std::sort_heap(v.begin(), v.end());
 
    std::cout << "\nsorted:\t";
    for (const auto &i : v) {                                                   
        std::cout << i << ' ';
    }   
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
heap:   9 4 5 1 1 3 
sorted: 1 1 3 4 5 9

二次

另见

make_heap

creates a max heap out of a range of elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com