cs11_c++_lab5待修改
heap.hh
- #ifndef HEAP_HH
- #define HEAP_HH
- #include <iostream>
- #include <stdexcept>
- #include <cassert>
- using namespace std;
- template <typename T, int maxsize>class heap//模板类heap,类型T,常量maxsize
- {
- private:
- int num_values;//当前存储的元素的数量
- T values[maxsize];//存储元素的数组
- int parent(int index)
- {
- return (index - ) / ;
- }
- int left_child(int index)
- {
- return * index + ;
- }
- int right_child(int index)
- {
- return * index + ;
- }
- void sift_down(int index)
- {
- assert(index < num_values);
- int left = left_child(index);
- int right = right_child(index);
- if(left >= num_values)
- {
- return;
- }
- if(right >= num_values)
- {
- if(values[left] < values[index])
- {
- swap_values(index, left);
- }
- }
- else
- {
- T left_value = values[left];
- T right_value = values[right];
- int swap_child;
- if(left_value < values[index] || right_value < values[index])
- {
- if(left_value < right_value)
- {
- swap_child = left;
- }
- else
- swap_child = right;
- swap_values(index, swap_child);
- sift_down(swap_child);
- }
- }
- }
- void sift_up(int index)
- {
- int parent_index = parent(index);
- if(index == )
- {
- return;
- }
- assert(parent_index >= );
- assert(parent_index != index);
- if(values[index] < values[parent_index])
- {
- swap_values(index, parent_index);
- if(parent_index != )
- {
- sift_up(parent_index);
- }
- }
- }
- void swap_values(int i, int j)
- {
- T tmp;
- assert(i >= && i < num_values);
- assert(j >= && j < num_values);
- assert(i != j);
- tmp = values[i];
- values[i] = values[j];
- values[j] = tmp;
- }
- public:
- heap<T, maxsize>():num_values(){cout<<"初始化啦"<<endl;}//初始化,在模板中的内容就无需再初始化了,只要初始化非模板内容就可以了。
- T get_first_value()
- {
- T result;
- try
- {
- if(num_values == )
- {
- throw std::underflow_error("抛出异常:堆为空");
- result = values[];
- --num_values;
- if(num_values != )
- {
- values[] = values[num_values];
- sift_down();
- }
- return result;
- }
- }
- catch (std::underflow_error &e)
- {
- cout << "捕捉异常:堆为空" << endl;
- }
- }
- void add_value(T value)
- {
- try
- {
- if(num_values >= maxsize)
- {
- throw std::overflow_error("抛出异常:堆为满");
- }
- values[num_values] = value;
- ++num_values;
- }
- catch (std::overflow_error &e)
- {
- cout << "捕捉异常:堆为满" << endl;
- }
- if((num_values - )> )
- sift_up(num_values - );
- }
- T get_value(int index)
- {
- try
- { if(index >= maxsize)
- {
- throw std::overflow_error("抛出异常:访问越界");
- }
- return values[index];
- }
- catch (std::overflow_error &e)
- {
- cout << "捕捉异常:访问越界" << endl;
- }
- }
- };
- #endif
xyz.cc
- #include <iostream>
- #include <string>
- #include "heap.hh"
- #include <stdlib.h>
- using namespace std;
- int main()
- {
- heap<int, >heapone;
- for(int i = ; i < ; i++)
- {
- heapone.add_value(i);
- }
- // int lastval = heapone.get_first_value();
- // cout<<"value0 = "<<lastval<<endl;;
- for(int i = ; i < ; i++)
- {
- cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;
- // cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
- }
- cout<<heapone.get_first_value()<<endl;
- for(int i = ; i < ; i++)
- {
- cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;
- cout<<heapone.get_first_value()<<endl;
- for(int i = ; i < ; i++)
- {
- cout<<"value"<<i<<" = "<<heapone.get_value(i)<<endl;
- // cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
- }
- cout<<"value"<<i<<" = "<<heapone.get_first_value()<<endl;
- }
- }
cs11_c++_lab5待修改的更多相关文章
- 一步步开发自己的博客 .NET版(11、Web.config文件的读取和修改)
Web.config的读取 对于Web.config的读取大家都很属性了.平时我们用得比较多的就是appSettings节点下配置.如: 我们对应的代码是: = ConfigurationManage ...
- 一次修改闭源 Entity Provider 程序集以兼容新 EntityFramework 的过程
读完本文你会知道,如何在没有源码的情况下,直接修改一个 DLL 以去除 DLL 上的强命名限制,并在该程序集上直接添加你的“友元程序集(一种特殊的 Attribute,将它应用在程序集上,使得程序集内 ...
- 当忘记mysql数据库密码时如何进行修改
因为长时间没有使用数据库了,或者把密码改完之后就忘了数据库密码,不能正常进入数据库,也无法修改密码,有一个简单的常用修改密码方式: 1.首先找到和打开mysql.exe和mysqld.exe所在的文件 ...
- DB1:数据库的创建和文件的修改
在SQL Server中,使用Create Database创建数据库,使用Alter Database命令,能够修改数据库的数据文件和日志文件. 一,创建数据库 1,在创建数据库时,最佳实践是: 创 ...
- ExtJS 4.2 业务开发(三)数据添加和修改
接上面的船舶管理业务,这里介绍添加和修改操作. 目录 1. 添加操作 2. 修改操作 3. 在线演示 1. 添加操作 1.1 创建AddShipWindow.js 在业务中的view目录下创建一个Ad ...
- 修改eclipse皮肤
习惯了vim黑色背景的程序猿们想必用eclipse时会倍感的不适应吧,不过没关系,因为eclipse的皮肤是可以自己定制的! 下面是我电脑上的eclipse界面,看到这个是不是找回了vim的感觉呢? ...
- ubuntu系统下如何修改host
Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改这个就可以了,结果发现是需要修改hosts.修改完之后要重启网络.具体过程 ...
- linux centos中添加删除修改环境变量,设置java环境变量
前言 安装完软件必要添加环境变量.指令很少,然而长时间不写就会不自信:我写的对吗?于是百度开始,于是发现又是各有千秋.好吧,好记星不如烂笔头.当然,最重要的是,百度出来的都他妈的是如何添加环境变量,只 ...
- 修改session垃圾回收几率
<?php //修改session垃圾回收几率 ini_set('session.gc_probability','1'); ini_set('session.gc_divisor','2'); ...
随机推荐
- 2016HUAS_ACM暑假集训4A - 递推
利用组合公式C(n,m)=C(n-1,m)+C(n-1,m-1).也就是从n个数里面选择m个数.按递增方式放在每一层循环. 杨辉三角+二项式定理,还真是挺有“意思”的一道题.说实话,非原创.见谅... ...
- pyside 添加菜单栏,窗口状态栏,工具栏
这三个放到一起,个人认为比较有可比性. 另外该写的解释我都记到注释里面了 话不多说,show me the code 菜单栏, # ubuntu16.04触发关联事件不成功,应该是ubantu的全局窗 ...
- Linux-TCP Queue的一些问题
先来回顾下三次握手里面涉及到的问题:1. 当 client 通过 connect 向 server 发出 SYN 包时,client 会维护一个 socket 等待队列,而 server 会维护一个 ...
- git代理,windows命令行代理,linux命令行代理
下载不动设置代理:git config --global http.proxy http://127.0.0.1:1080git config --global https.proxy https:/ ...
- wine
sudo dpkg --add-architecture i386 sudo add-apt-repository ppa:wine/wine-buildssudo apt-get update su ...
- virtio-blk简介[转]
声明: 本博客欢迎转发,但请保留原作者信息!新浪微博:@孔令贤HW: 博客地址:http://lingxiankong.github.io/内容系本人学习.研究和总结,如有雷同,实属荣幸! virti ...
- js,addEventListener参数传递
解决方法 因为i相对匿名函数是外面的变量,就把循环绑定的时候,将i的值传入到匿名函数内,就可以了.因此需要在匿名函数(事件函数)外包裹一个匿名函数, 并立即执行. var elems = docume ...
- 【转】Java代码规范
[转]Java代码规范 http://blog.csdn.net/huaishu/article/details/26725539
- js api 实现钉钉免登
js api 实现钉钉免登,用于从钉钉微应用跳转到企业内部的oa,erp等,我刚刚实施完了我公司的这个功能,钉钉用起来还不错. 1 js api 实现钉钉免登,页面配置. <title>利 ...
- R语言-数据高级管理
数学函数 abs() 绝对值 sqrt() 平方 ceiling() 向上取整 floor() 向下取整 trunc() 截取整数部分 round(x,digits = n) 保留几位小数 统计函数 ...