算法之旅,直奔<algorithm>之二十 make_heap

来源:互联网 发布:excel如何拆分数据公式 编辑:程序博客网 时间:2024/06/13 21:34

make_heap(vs2010)

  • 引言
这是我学习总结<algorithm>的第二十篇, make_heap 还是比较常用的。。。就是建堆
  • 作用
make_heap 的作用建堆,给一数据集合,然后建堆。
  • 原型

  • 实验
数据集合,如下
建堆后,输出堆顶
30
堆顶弹出
输出堆顶
20
插入99,
输出堆顶
99
然后堆排序
5 10 15 20 99
  • 代码
test.cpp
#include <iostream>     // std::cout#include <algorithm>    // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap#include <vector>       // std::vectorint main () {int myints[] = {10,20,30,5,15};std::vector<int> v(myints,myints+5);std::make_heap (v.begin(),v.end());std::cout << "initial max heap   : " << v.front() << '\n';std::pop_heap (v.begin(),v.end()); v.pop_back();std::cout << "max heap after pop : " << v.front() << '\n';v.push_back(99); std::push_heap (v.begin(),v.end());std::cout << "max heap after push: " << v.front() << '\n';std::sort_heap (v.begin(),v.end());std::cout << "final sorted range :";for (unsigned i=0; i<v.size(); i++)std::cout << ' ' << v[i];std::cout << '\n';system("pause");return 0;}


0 0
原创粉丝点击