我有这段代码,它只是堆类。现在尝试将动态数组转换为类堆,然后将其打印出来,我为此使用了指针,因为它是不允许的
#include <assert.h>
#include <iostream>
using namespace std;
class heap
{
public:
heap(int size);
~heap();
int add_element(int element);
int get_min();
void arr_to_q(int *arr, int len);
void show();
/* data */
private:
void down(int i, int len);
int* Buffer;
int mysize;
int currentsize;
void up(int i, int len);
void reallocate();
};
heap::heap(int size){
int* Buffer=new int[size];
currentsize=size;
mysize=size;
cout<<mysize;
}
heap::~heap(){
}
void heap::arr_to_q(int *arr, int len){
cout<<currentsize;
currentsize+=len;
if (len>mysize){
reallocate();
}
for (int i=len-1; i>=0; i--){
up(i, len);
}
}
void heap::reallocate(){
int *sbuffer=Buffer;
int *Buffer = new int[mysize*2];
mysize*=2;
for (int i=0; i<mysize; i++){
Buffer[i]=sbuffer[i];
}
delete [] sbuffer;
}
void heap::up(int i, int len){
int par, sosed, min;
if (i%2==0){
par=i/2-1;
sosed=i-1;
}
else{
par=i/2;
sosed=sosed+1;
}
if (Buffer[i]<Buffer[sosed]){
min=i;
}
else{
min=sosed;
}
if (Buffer[min]<Buffer[par]){
int c = Buffer[min];
Buffer[min]=Buffer[par];
Buffer[par]=c;
up(min, len);
}
}
void heap::show(){
for(int i=0; i<currentsize; i++){
cout<<"You queue is"<<Buffer[i];
// cout<<"currentsize: "<<currentsize;
}
}
int main(){
int len;
printf("Vvedite dlinu massiva");
scanf("%d", &len);
int *arr=new int[len];
for(int i=0; i<len;i++){
printf("Vvedite %d element\n", i);
scanf("%d", &arr[i]);
}
heap heaps(5);
heaps.arr_to_q(arr, len);
// heaps.show();
delete [] arr;
但是在启动后它会因 sigmentation fault 的错误而崩溃 11. 我的错误在哪里?先感谢您。