Namespaces
Variants

std:: sort_heap

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
sort_heap
(C++11)
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class RandomIt >
void sort_heap ( RandomIt first, RandomIt last ) ;
(1) (自 C++20 起为 constexpr)
template < class RandomIt, class Compare >
void sort_heap ( RandomIt first, RandomIt last, Compare comp ) ;
(2) (自 C++20 起为 constexpr)

[ first , last ) 转换为已排序的范围。此时不再保持堆属性。

1) 堆是相对于 operator < (C++20 前) std:: less { } (C++20 起) 构建的,并将相对于 operator < (C++20 前) std:: less { } (C++20 起) 进行排序。
2) 堆是基于 comp 构建的,并将按照 comp 进行排序。

若满足以下任一条件,则行为未定义:

  • [ first , last ) 不是一个堆。
(C++11 前)
(C++11 起)

目录

参数

first, last - 定义待排序 范围 的二叉堆元素迭代器对
comp - 比较函数对象(即满足 Compare 要求的对象),当第一个参数 小于 第二个参数时返回 true

比较函数的签名应等价于如下形式:

bool cmp ( const Type1 & a, const Type2 & b ) ;

虽然签名不需要包含 const & ,但函数不得修改传递的对象,且必须能够接受所有 Type1 Type2 类型的值(包括const类型),无论其 值类别 为何(因此不允许使用 Type1 & ,也不允许使用 Type1 ,除非对于 Type1 类型移动操作与拷贝操作等效 (C++11 起) )。
类型 Type1 Type2 必须使得 RandomIt 类型的对象在解引用后能隐式转换到这两个类型。

类型要求
-
RandomIt 必须满足 LegacyRandomAccessIterator 的要求。
-
Compare 必须满足 Compare 的要求。

复杂度

给定 N std:: distance ( first, last )

1) 最多进行 2N⋅log(N) 次比较,使用 operator < (C++20 前) std:: less { } (C++20 起)
2) 最多 2N⋅log(N) 次比较函数 comp 的应用。

可能的实现

堆排序 (1)
template<class RandomIt>
void sort_heap(RandomIt first, RandomIt last)
{
    while (first != last)
        std::pop_heap(first, last--);
}
堆排序 (2)
template<class RandomIt, class Compare>
void sort_heap(RandomIt first, RandomIt last, Compare comp)
{
    while (first != last)
        std::pop_heap(first, last--, comp);
}

示例

#include <algorithm>
#include <iostream>
#include <string_view>
#include <vector>
void println(std::string_view fmt, const auto& v)
{
    for (std::cout << fmt; const auto &i : v)
        std::cout << i << ' ';
    std::cout << '\n';
}
int main()
{
    std::vector<int> v{3, 1, 4, 1, 5, 9};
    std::make_heap(v.begin(), v.end());
    println("after make_heap, v: ", v);
    std::sort_heap(v.begin(), v.end());
    println("after sort_heap, v: ", v);
}

输出:

after make_heap, v: 9 4 5 1 1 3
after sort_heap, v: 1 1 3 4 5 9

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2444 C++98 最多允许 N⋅log(N) 次比较 提升至 2N⋅log(N) 次比较

参见

(C++11)
检查给定范围是否构成最大堆
(函数模板)
寻找构成最大堆的最大子范围
(函数模板)
从元素范围创建最大堆
(函数模板)
从最大堆中移除最大元素
(函数模板)
向最大堆添加元素
(函数模板)
将最大堆转换为按升序排序的元素范围
(算法函数对象)