本文主要是介绍C++中堆的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C++中堆的应用:make_heap, pop_heap, push_heap, sort_heap, priority_queue
make_heap, pop_heap, push_heap, sort_heap都是标准算法库里的模板函数,用于将存储在vector/deque 中的元素进行堆操作,对不愿自己写数据结构堆的C++选手来说,这几个算法函数很有用,下面是这几个函数操作vector中元素的例子。详细解释可以参见: http://www.cplusplus.com/reference/algorithm/push_heap/
Push element into heap range
Given a heap range
A range can be made into a heap by calling function
Parameters
- first, last
- Random-Access iterators to the initial and final positions of the new heap, including the pushed element. The range used is
[first,last), which contains all the elements between first andlast, including the element pointed by first but not the element pointed by last. comp - Comparison function object that, taking two values of the same type than those contained in the range, returns
true if the first argument goes before the second argument in the specificstrict weak ordering it defines, and false otherwise.
Return value
none
Example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
}
Output:
max heap after pop : 20
max heap after push: 99
final sorted range : 5 10 15 20 99
这篇关于C++中堆的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!