我是靠谱客的博主 强健香氛,最近开发中收集的这篇文章主要介绍C++ STL 堆算法: make_heap(), push_heap(), pop_heap(), sort_heap(),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

在c++20中引入了几个堆相关的算法:make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until(),现在来开始介绍这几个函数和写一个示例。

make_heap在一个迭代器范围内构造一个堆(默认最大堆)
push_heap往一个元素插入到堆的末尾的下一个位置
pop_heap弹出堆顶元素,并把它放到末尾位置
sort_heap把一个堆进行排序
is_heap 
is_heap_until 

 

 

make_heap(),pop_heap(), sort_heap()的使用示例

#include <algorithm>
#include <functional>
#include <iostream>
#include <string_view>
#include <vector>
 
void print(std::string_view text, std::vector<int> const& 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);
    sort_heap(v.begin(), v.end(), std::less<>());
    print("after sort_heap", 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);
    sort_heap(v.begin(), v.end(), std::greater<>());
    print("after sort_heap", v);
    
}

输出如下:

Max heap: 
initially, v: 3 2 4 1 5 9 
after make_heap, v: 9 5 4 1 2 3 
after pop_heap, v: 5 3 4 1 2 9 
former top element: 9 
after removing the former top element, v: 5 3 4 1 2 
after sort_heap: 1 2 3 4 5 
 
Min heap: 
initially, v1: 3 2 4 1 5 9 
after make_heap, v1: 1 2 4 3 5 9 
after pop_heap, v1: 2 3 4 9 5 1 
former top element: 1 
after removing the former top element, v1: 2 3 4 9 5 
after sort_heap: 5 4 3 2 1

 

  push_heap()示例

#include <iostream>
#include <algorithm>
#include <vector>
 
int main()
{
    std::vector<int> v { 3, 1, 4, 1, 5, 9 };
 
    std::make_heap(v.begin(), v.end());
 
    std::cout << "v: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << 'n';
 
    v.push_back(6);
 
    std::cout << "before push_heap: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << 'n';
 
    std::push_heap(v.begin(), v.end());
 
    std::cout << "after push_heap:  ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << 'n';
}
v: 9 5 4 1 1 3 
before push_heap: 9 5 4 1 1 3 6 
after push_heap:  9 5 6 1 1 3 4

最后

以上就是强健香氛为你收集整理的C++ STL 堆算法: make_heap(), push_heap(), pop_heap(), sort_heap()的全部内容,希望文章能够帮你解决C++ STL 堆算法: make_heap(), push_heap(), pop_heap(), sort_heap()所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部