c++学习连载-堆排序学习及遇到问题
2013-12-04 11:25
351 查看
我首先写了一个heap类;
下面是一个不完整的heap类:
#include <iostream>
#ifndef _HEAP_H_
#define _HEAP_H_
using namespace std;
class heap
{
public:
heap()
{
}
int HeapPar(int i)
{
return i/2;
}
int HeapLeft(int i)
{
return 2*i;
}
int HeapRight(int i)
{
return 2*i+1;
}
template <class T>
int getArrayLen(T& array)
{//使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
return (sizeof(array) / sizeof(array[0]));
}
void InHeat(int A[]);
void MaxHeapify(int i);
int* Pheap;
int HeapSize;
~heap()
{
delete [] Pheap;
}
}
#endif
注意到没有,heap类的定义后面我没有写分号
接下来是两个成员函数的实现文件
#include"heap.h"
void heap::InHeat(int A[])
{
HeapSize=getArrayLen(A);
Pheap=new int[HeapSize];
}
#include"heap.h"
void heap::MaxHeapify(int i)
{
int l,r,largest;
int temp;
l=HeapLeft(i);
r=HeapRight(i);
if((l<=HeapSize)&&(Pheap[l-1]>Pheap[i-1]))
{
largest=l;
}
else
{
largest=i;
}
if((r<=HeapSize)&&(Pheap[r-1]>Pheap[largest-1]))
{
largest=r;
}
if(largest!=i)
{
temp=Pheap[i-1];
Pheap[i-1]=Pheap[largest-1];
Pheap[largest-1]=temp;
heap::MaxHeapify(largest);
}
}
下面是对上面两个成员函数文件单独编译的结果:
由于类的定义少了个分号,所以会出现上面的编译错误。
另外刚开始的时候,我没有把int getArrayLen(T& array)写在类里面作为类的成员函数
而是作为全局函数在类外定义的,所以当我用类的成员函数调用它时,老报错
所以我总结了一下,类的成员函数不能调用全局函数的。
下面是完整的heap类
#include <iostream>
#ifndef _HEAP_H_
#define _HEAP_H_
using namespace std;
class heap
{
public:
heap()
{
}
int HeapPar(int i)
{
return i/2;
}
int HeapLeft(int i)
{
return 2*i;
}
int HeapRight(int i)
{
return 2*i+1;
}
template <class T>
int getArrayLen(T& array)
{//使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
return (sizeof(array) / sizeof(array[0]));
}
void InHeat(int A[]);
void MaxHeapify(int i);
void BuildMapHeap();
int* Pheap;
int HeapSize;
~heap()
{
delete [] Pheap;
}
};
#endif
#include"heap.h"
void heap::MaxHeapify(int i)
{
int l,r,largest;
int temp;
l=HeapLeft(i);
r=HeapRight(i);
if((l<=HeapSize)&&(Pheap[l-1]>Pheap[i-1]))
{
largest=l;
}
else
{
largest=i;
}
if((r<=HeapSize)&&(Pheap[r-1]>Pheap[largest-1]))
{
largest=r;
}
if(largest!=i)
{
temp=Pheap[i-1];
Pheap[i-1]=Pheap[largest-1];
Pheap[largest-1]=temp;
heap::MaxHeapify(largest);
}
}
#include"heap.h"
void heap::InHeat(int A[])
{
HeapSize=getArrayLen(A);
Pheap=new int[HeapSize];
}
#include"heap.h"
void heap::BuildMapHeap()
{
for(int i=heap::HeapSize/2;i>=1;i--)
{
heap::MaxHeapify(i);
}
}
下面是一个不完整的heap类:
#include <iostream>
#ifndef _HEAP_H_
#define _HEAP_H_
using namespace std;
class heap
{
public:
heap()
{
}
int HeapPar(int i)
{
return i/2;
}
int HeapLeft(int i)
{
return 2*i;
}
int HeapRight(int i)
{
return 2*i+1;
}
template <class T>
int getArrayLen(T& array)
{//使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
return (sizeof(array) / sizeof(array[0]));
}
void InHeat(int A[]);
void MaxHeapify(int i);
int* Pheap;
int HeapSize;
~heap()
{
delete [] Pheap;
}
}
#endif
注意到没有,heap类的定义后面我没有写分号
接下来是两个成员函数的实现文件
#include"heap.h"
void heap::InHeat(int A[])
{
HeapSize=getArrayLen(A);
Pheap=new int[HeapSize];
}
#include"heap.h"
void heap::MaxHeapify(int i)
{
int l,r,largest;
int temp;
l=HeapLeft(i);
r=HeapRight(i);
if((l<=HeapSize)&&(Pheap[l-1]>Pheap[i-1]))
{
largest=l;
}
else
{
largest=i;
}
if((r<=HeapSize)&&(Pheap[r-1]>Pheap[largest-1]))
{
largest=r;
}
if(largest!=i)
{
temp=Pheap[i-1];
Pheap[i-1]=Pheap[largest-1];
Pheap[largest-1]=temp;
heap::MaxHeapify(largest);
}
}
下面是对上面两个成员函数文件单独编译的结果:
由于类的定义少了个分号,所以会出现上面的编译错误。
另外刚开始的时候,我没有把int getArrayLen(T& array)写在类里面作为类的成员函数
而是作为全局函数在类外定义的,所以当我用类的成员函数调用它时,老报错
所以我总结了一下,类的成员函数不能调用全局函数的。
下面是完整的heap类
#include <iostream>
#ifndef _HEAP_H_
#define _HEAP_H_
using namespace std;
class heap
{
public:
heap()
{
}
int HeapPar(int i)
{
return i/2;
}
int HeapLeft(int i)
{
return 2*i;
}
int HeapRight(int i)
{
return 2*i+1;
}
template <class T>
int getArrayLen(T& array)
{//使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
return (sizeof(array) / sizeof(array[0]));
}
void InHeat(int A[]);
void MaxHeapify(int i);
void BuildMapHeap();
int* Pheap;
int HeapSize;
~heap()
{
delete [] Pheap;
}
};
#endif
#include"heap.h"
void heap::MaxHeapify(int i)
{
int l,r,largest;
int temp;
l=HeapLeft(i);
r=HeapRight(i);
if((l<=HeapSize)&&(Pheap[l-1]>Pheap[i-1]))
{
largest=l;
}
else
{
largest=i;
}
if((r<=HeapSize)&&(Pheap[r-1]>Pheap[largest-1]))
{
largest=r;
}
if(largest!=i)
{
temp=Pheap[i-1];
Pheap[i-1]=Pheap[largest-1];
Pheap[largest-1]=temp;
heap::MaxHeapify(largest);
}
}
#include"heap.h"
void heap::InHeat(int A[])
{
HeapSize=getArrayLen(A);
Pheap=new int[HeapSize];
}
#include"heap.h"
void heap::BuildMapHeap()
{
for(int i=heap::HeapSize/2;i>=1;i--)
{
heap::MaxHeapify(i);
}
}
相关文章推荐
- c++学习连载-插入排序及其遇到的问题
- C++学习连载—合并排序及其遇到的问题
- C++ 学习拾遗 —— 点滴记录C++学习过程中遇到的问题以及整理
- 学习C++遇到的问题
- 学习C++中遇到的各种问题
- c++ 学习中遇到的问题: error C2065: 'ifstream' : undeclared identifier
- C++ 学习拾遗 —— 点滴记录C++学习过程中遇到的问题以及整理
- 学习C++ GUI Programming with Qt 4遇到的问题(1)
- c++ 学习中遇到的问题:ifstream打开指定路径时,显示路劲错误
- C++学习中遇到的问题及其解决方案
- C++学习中关于cout遇到的一个小问题
- Android NDK学习(7)NDK测试时遇到的问题:C与C++互相调用
- C++学习中遇到的两个问题
- C++学习中遇到的问题及其解决方案
- 学习中遇到的c++问题,持续更新
- C++学习遇到的问题
- C++学习中遇到的问题(求解答)
- c++学习连载-堆排序完整程序
- 学习中遇到的c++问题,持续更新
- JAVA视频系列,学习JAVA常遇到的问题,连载【方法篇】。