Namespaces
Variants

std:: make_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
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class RandomIt >
void make_heap ( RandomIt first, RandomIt last ) ;
(1) (自 C++20 起为 constexpr)
template < class RandomIt, class Compare >
void make_heap ( RandomIt first, RandomIt last, Compare comp ) ;
(2) (自 C++20 起为 constexpr)

在范围 [ first , last ) 内构建一个

1) 所构建的堆是基于 operator < (C++20 前) std:: less { } (C++20 起) 的。
2) 所构建的堆是相对于 comp 而言的。

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

(C++11 前)
(C++11 起)

目录

参数

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

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

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

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

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

复杂度

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

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

示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <string_view>
#include <vector>
void print(std::string_view text, const std::vector<int>& v = {})
{
    std::cout << text << ": ";
    for (const auto& e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    print("Max heap");
    std::vector<int> v{3, 2, 4, 1, 5, 9};
    print("initially, v", v);
    std::make_heap(v.begin(), v.end());
    print("after make_heap, v", v);
    std::pop_heap(v.begin(), v.end());
    print("after pop_heap, v", v);
    auto top = v.back();
    v.pop_back();
    print("former top element", {top});
    print("after removing the former top element, v", v);
    print("\nMin heap");
    std::vector<int> v1{3, 2, 4, 1, 5, 9};
    print("initially, v1", v1);
    std::make_heap(v1.begin(), v1.end(), std::greater<>{});
    print("after make_heap, v1", v1);
    std::pop_heap(v1.begin(), v1.end(), std::greater<>{});
    print("after pop_heap, v1", v1);
    auto top1 = v1.back();
    v1.pop_back();
    print("former top element", {top1});
    print("after removing the former top element, v1", v1);
}

输出:

最大堆:
初始状态,v:3 2 4 1 5 9
执行 make_heap 后,v:9 5 4 1 2 3
执行 pop_heap 后,v:5 3 4 1 2 9
原顶部元素:9
移除原顶部元素后,v:5 3 4 1 2
最小堆:
初始状态,v1:3 2 4 1 5 9
执行 make_heap 后,v1:1 2 4 3 5 9
执行 pop_heap 后,v1:2 3 4 9 5 1
原顶部元素:1
移除原顶部元素后,v1:2 3 4 9 5

缺陷报告

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

缺陷报告 应用于 发布时行为 正确行为
LWG 3032 C++98 未要求 [ first , last ) 范围内的元素可交换 要求可交换

参见

(C++11)
检查给定范围是否构成最大堆
(函数模板)
查找构成最大堆的最大子范围
(函数模板)
向最大堆添加元素
(函数模板)
从最大堆中移除最大元素
(函数模板)
将最大堆转换为按升序排序的元素范围
(函数模板)
适配容器以提供优先队列功能
(类模板)
从元素范围创建最大堆
(算法函数对象)