大堆最小堆的创建与应用"/>
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++ 最大堆最小堆的创建与应用
发布评论