Is there an easy way to make a min heap in C++?(有没有一种简单的方法可以在 C++ 中创建最小堆?)
本文介绍了有没有一种简单的方法可以在 C++ 中创建最小堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 C++ 很陌生,我想知道是否有办法从标准库中使用 C++ 生成最小堆.
I'm very new to C++, and I was wondering if there was a way to make a min heap in C++ from the standard library.
推荐答案
使用make_heap()和朋友,定义在中,或者使用priority_queue,定义在 中.priority_queue 使用 make_heap 和下面的朋友.
Use make_heap() and friends, defined in <algorithm>, or use priority_queue, defined in <queue>. The priority_queue uses make_heap and friends underneath.
#include <queue> // functional,iostream,ctime,cstdlib
using namespace std;
int main(int argc, char* argv[])
{
srand(time(0));
priority_queue<int,vector<int>,greater<int> > q;
for( int i = 0; i != 10; ++i ) q.push(rand()%10);
cout << "Min-heap, popped one by one: ";
while( ! q.empty() ) {
cout << q.top() << ' '; // 0 3 3 3 4 5 5 6 8 9
q.pop();
}
cout << endl;
return 0;
}
这篇关于有没有一种简单的方法可以在 C++ 中创建最小堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:有没有一种简单的方法可以在 C++ 中创建最小堆?
基础教程推荐
猜你喜欢
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
