heap.hh

  1. #ifndef HEAP_HH
  2. #define HEAP_HH
  3.  
  4. #include <iostream>
  5. #include <stdexcept>
  6. #include <cassert>
  7.  
  8. using namespace std;
  9.  
  10. template <typename T, int maxsize>class heap//模板类heap,类型T,常量maxsize
  11. {
  12. private:
  13. int num_values;//当前存储的元素的数量
  14. T values[maxsize];//存储元素的数组
  15.  
  16. int parent(int index)
  17. {
  18. return (index - ) / ;
  19. }
  20.  
  21. int left_child(int index)
  22. {
  23. return * index + ;
  24. }
  25.  
  26. int right_child(int index)
  27. {
  28. return * index + ;
  29. }
  30.  
  31. void sift_down(int index)
  32. {
  33. assert(index < num_values);
  34. int left = left_child(index);
  35. int right = right_child(index);
  36.  
  37. if(left >= num_values)
  38. {
  39. return;
  40. }
  41. if(right >= num_values)
  42. {
  43. if(values[left] < values[index])
  44. {
  45. swap_values(index, left);
  46. }
  47. }
  48. else
  49. {
  50. T left_value = values[left];
  51. T right_value = values[right];
  52. int swap_child;
  53.  
  54. if(left_value < values[index] || right_value < values[index])
  55. {
  56. if(left_value < right_value)
  57. {
  58. swap_child = left;
  59. }
  60. else
  61. swap_child = right;
  62. swap_values(index, swap_child);
  63. sift_down(swap_child);
  64. }
  65. }
  66. }
  67.  
  68. void sift_up(int index)
  69. {
  70. int parent_index = parent(index);
  71. if(index == )
  72. {
  73. return;
  74. }
  75. assert(parent_index >= );
  76. assert(parent_index != index);
  77. if(values[index] < values[parent_index])
  78. {
  79. swap_values(index, parent_index);
  80. if(parent_index != )
  81. {
  82. sift_up(parent_index);
  83. }
  84. }
  85. }
  86.  
  87. void swap_values(int i, int j)
  88. {
  89. T tmp;
  90.  
  91. assert(i >= && i < num_values);
  92. assert(j >= && j < num_values);
  93. assert(i != j);
  94.  
  95. tmp = values[i];
  96. values[i] = values[j];
  97. values[j] = tmp;
  98. }
  99.  
  100. public:
  101. heap<T, maxsize>():num_values(){cout<<"初始化啦"<<endl;}//初始化,在模板中的内容就无需再初始化了,只要初始化非模板内容就可以了。
  102.  
  103. T get_first_value()
  104. {
  105. T result;
  106. try
  107. {
  108. if(num_values == )
  109. {
  110. throw std::underflow_error("抛出异常:堆为空");
  111. result = values[];
  112. --num_values;
  113. if(num_values != )
  114. {
  115. values[] = values[num_values];
  116. sift_down();
  117. }
  118. return result;
  119. }
  120. }
  121. catch (std::underflow_error &e)
  122. {
  123. cout << "捕捉异常:堆为空" << endl;
  124. }
  125. }
  126.  
  127. void add_value(T value)
  128. {
  129. try
  130. {
  131. if(num_values >= maxsize)
  132. {
  133. throw std::overflow_error("抛出异常:堆为满");
  134. }
  135. values[num_values] = value;
  136. ++num_values;
  137. }
  138. catch (std::overflow_error &e)
  139. {
  140. cout << "捕捉异常:堆为满" << endl;
  141. }
  142.  
  143. if((num_values - )> )
  144. sift_up(num_values - );
  145. }
  146.  
  147. T get_value(int index)
  148. {
  149. try
  150. { if(index >= maxsize)
  151. {
  152. throw std::overflow_error("抛出异常:访问越界");
  153. }
  154. return values[index];
  155. }
  156. catch (std::overflow_error &e)
  157. {
  158. cout << "捕捉异常:访问越界" << endl;
  159. }
  160. }
  161. };
  162.  
  163. #endif

xyz.cc

  1. #include <iostream>
  2. #include <string>
  3. #include "heap.hh"
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. heap<int, >heapone;
  10.  
  11. for(int i = ; i < ; i++)
  12. {
  13. heapone.add_value(i);
  14. }
  15.  
  16. // int lastval = heapone.get_first_value();
  17. // cout<<"value0 = "<<lastval<<endl;;
  18.  
  19. for(int i = ; i < ; i++)
  20. {
  21. cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;
  22.  
  23. // cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
  24.  
  25. }
  26.  
  27. cout<<heapone.get_first_value()<<endl;
  28. for(int i = ; i < ; i++)
  29. {
  30. cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;
  31.  
  32. cout<<heapone.get_first_value()<<endl;
  33. for(int i = ; i < ; i++)
  34. {
  35. cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;
  36.  
  37. // cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
  38.  
  39. }
  40. cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
  41.  
  42. }
  43.  
  44. }

cs11_c++_lab5待修改的更多相关文章

  1. 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)

    Web.config的读取 对于Web.config的读取大家都很属性了.平时我们用得比较多的就是appSettings节点下配置.如: 我们对应的代码是: = ConfigurationManage ...

  2. 一次修改闭源 Entity Provider 程序集以兼容新 EntityFramework 的过程

    读完本文你会知道,如何在没有源码的情况下,直接修改一个 DLL 以去除 DLL 上的强命名限制,并在该程序集上直接添加你的“友元程序集(一种特殊的 Attribute,将它应用在程序集上,使得程序集内 ...

  3. 当忘记mysql数据库密码时如何进行修改

    因为长时间没有使用数据库了,或者把密码改完之后就忘了数据库密码,不能正常进入数据库,也无法修改密码,有一个简单的常用修改密码方式: 1.首先找到和打开mysql.exe和mysqld.exe所在的文件 ...

  4. DB1:数据库的创建和文件的修改

    在SQL Server中,使用Create Database创建数据库,使用Alter Database命令,能够修改数据库的数据文件和日志文件. 一,创建数据库 1,在创建数据库时,最佳实践是: 创 ...

  5. ExtJS 4.2 业务开发(三)数据添加和修改

    接上面的船舶管理业务,这里介绍添加和修改操作. 目录 1. 添加操作 2. 修改操作 3. 在线演示 1. 添加操作 1.1 创建AddShipWindow.js 在业务中的view目录下创建一个Ad ...

  6. 修改eclipse皮肤

    习惯了vim黑色背景的程序猿们想必用eclipse时会倍感的不适应吧,不过没关系,因为eclipse的皮肤是可以自己定制的! 下面是我电脑上的eclipse界面,看到这个是不是找回了vim的感觉呢? ...

  7. ubuntu系统下如何修改host

    Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改这个就可以了,结果发现是需要修改hosts.修改完之后要重启网络.具体过程 ...

  8. linux centos中添加删除修改环境变量,设置java环境变量

    前言 安装完软件必要添加环境变量.指令很少,然而长时间不写就会不自信:我写的对吗?于是百度开始,于是发现又是各有千秋.好吧,好记星不如烂笔头.当然,最重要的是,百度出来的都他妈的是如何添加环境变量,只 ...

  9. 修改session垃圾回收几率

    <?php //修改session垃圾回收几率 ini_set('session.gc_probability','1'); ini_set('session.gc_divisor','2'); ...

随机推荐

  1. 2016HUAS_ACM暑假集训4A - 递推

    利用组合公式C(n,m)=C(n-1,m)+C(n-1,m-1).也就是从n个数里面选择m个数.按递增方式放在每一层循环. 杨辉三角+二项式定理,还真是挺有“意思”的一道题.说实话,非原创.见谅... ...

  2. pyside 添加菜单栏,窗口状态栏,工具栏

    这三个放到一起,个人认为比较有可比性. 另外该写的解释我都记到注释里面了 话不多说,show me the code 菜单栏, # ubuntu16.04触发关联事件不成功,应该是ubantu的全局窗 ...

  3. Linux-TCP Queue的一些问题

    先来回顾下三次握手里面涉及到的问题:1. 当 client 通过 connect 向 server 发出 SYN 包时,client 会维护一个 socket 等待队列,而 server 会维护一个 ...

  4. git代理,windows命令行代理,linux命令行代理

    下载不动设置代理:git config --global http.proxy http://127.0.0.1:1080git config --global https.proxy https:/ ...

  5. wine

    sudo dpkg --add-architecture i386 sudo add-apt-repository ppa:wine/wine-buildssudo apt-get update su ...

  6. virtio-blk简介[转]

    声明: 本博客欢迎转发,但请保留原作者信息!新浪微博:@孔令贤HW: 博客地址:http://lingxiankong.github.io/内容系本人学习.研究和总结,如有雷同,实属荣幸! virti ...

  7. js,addEventListener参数传递

    解决方法 因为i相对匿名函数是外面的变量,就把循环绑定的时候,将i的值传入到匿名函数内,就可以了.因此需要在匿名函数(事件函数)外包裹一个匿名函数, 并立即执行. var elems = docume ...

  8. 【转】Java代码规范

    [转]Java代码规范 http://blog.csdn.net/huaishu/article/details/26725539

  9. js api 实现钉钉免登

    js api 实现钉钉免登,用于从钉钉微应用跳转到企业内部的oa,erp等,我刚刚实施完了我公司的这个功能,钉钉用起来还不错. 1 js api 实现钉钉免登,页面配置. <title>利 ...

  10. R语言-数据高级管理

    数学函数 abs() 绝对值 sqrt() 平方 ceiling() 向上取整 floor() 向下取整 trunc() 截取整数部分 round(x,digits = n) 保留几位小数 统计函数 ...