C++ 最大堆最小堆的创建与应用

编程入门 行业动态 更新时间:2024-10-23 11:24:25

C++ 最<a href=https://www.elefans.com/category/jswz/34/1647321.html style=大堆最小堆的创建与应用"/>

C++ 最大堆最小堆的创建与应用

  • 代码
#include <iostream>
#include <queue>
#include <initializer_list>using namespace std;
int main(){//建堆priority_queue<int, vector<int>, greater<int> > minheap;//默认对堆是最大堆 等价于 priority_queue<int> maxheappriority_queue<int, vector<int>, less<int> > maxheap;//插入for(int n : {1,8,5,6,3,4,0,9,7,2}){minheap.push(n);maxheap.push(n);}//输出堆(取出堆顶元素,删除对应元素)cout<<"*********最小堆*********"<<endl;while(!minheap.empty()){cout<<minheap.top()<<" ";minheap.pop();}cout<<"\n\n\n\n*********最大堆*********"<<endl;while(!maxheap.empty()){cout<<maxheap.top()<<" ";maxheap.pop();}
}
  • 运行结果
  • 参考
    • C++官方文档

更多推荐

C++ 最大堆最小堆的创建与应用

本文发布于:2024-03-14 02:45:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1735456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大堆   最小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!