微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

指向结构体的数据结构

如何解决指向结构体的数据结构

我想从头开始构建一个指向 C++ 结构的优先级队列。我知道这可以通过 priority_queue <struct_name> pq 使用 STL 库轻松完成。但是,我有兴趣使用数组自己构建它。我最初的方法是为整数构建一个最小堆,并最终修改代码使数组指向一个结构。为了给你更多的上下文,这是我到目前为止编写的代码

#include<iostream>

#define MAX_SIZE 100
using namespace std;

struct cell{
    int x,y;
    int depth;
    
    // Constructor
    cell(int x,int y,int depth):
        x(x),y(y),depth(depth) {}
    
    
};

class minHeap{
private:
    // Current size of heap
    int size;
    // Max size of heap
    int capacity;
    // Storing elements as an array
    struct cell* heap = new struct cell[MAX_SIZE];
    // Returns the parent index
    int parent(int i){
        return (i-1)/2;
    }
    // Returns left child
    int leftChild(int i){
        return 2*i + 1;
    }
    // returns right child
    int rightChild(int i){
        return 2*i + 2;
    }
public:
    // Constructor
    minHeap(int capacity){
        size = 0;
        this->capacity = capacity;
        
    }
    
    
    void insert(struct cell node){
        if(size == capacity){
            cout<<"Heap is FULL"<<endl;
            return;
        }
        // Increase the amount of elements in the heap
        size++;
        
        // Insert the element at the end of heap
        heap[size-1] = node;
        
        // Maintain heap invariance
        int i = size - 1;
        while(i!=0 && greater(heap[parent(i)],heap[i])){
            struct cell temp = heap[i];
            heap[i] = heap[parent(i)];
            heap[parent(i)] = temp;
            i = parent(i);
        }
    }
    
    void heapify(int i){
        int left = leftChild(i);
        int right = rightChild(i);
        int smallest = i;
        
        // Find the smallest element of the three
        if((left < size) && greater(heap[left],heap[smallest])){
            smallest = left;
        }
        if((right < size) && (greater(heap[right],heap[smallest]))){
            smallest = right;
        }
        // If the smallest is left or right,keep heapifying
        if(smallest != i){
            struct cell temp = heap[i];
            heap[i] = heap[smallest];
            heap[smallest] = temp;
            heapify(smallest);
        }
        
    }
    
    int extractMin(){
        // Check is heap is empty
        if(size == 0){
            cout<<"Heap is empty"<<endl;
            return -1;
        }
        else if(size == 1){
            size--;
            return heap[0].depth;
        }
        else{
            struct cell root = heap[0];
            heap[0] = heap[size-1];
            size--;
            heapify(0);
            
            return root.depth;
        }
    }
    
    void printHeap(){
        int power = 0;
        int value = 0;
        for(int i=0; i<size; i++){
            if(i == value){
                cout<<endl;
                power += 1;
                value += (1<<power);
            }
            cout<<heap[i].depth<<" ";
        }
        cout<<endl;
    }
};

bool greater(struct cell cell1,struct cell cell2){
        if(cell1.depth == cell2.depth){
            if(cell1.x != cell2.depth){
                return (cell1.x < cell2.x);
            }
            else return cell1.y < cell2.y;
        }
        return cell1.depth < cell2.depth;
    }

int main(){
    minHeap m1(15);
    return 0;
}

但是,我在执行此操作后遇到了一些错误。我在下面列出了它们:

Heap.cpp:24:46: error: no matching function for call to 'cell::cell()'
  struct cell* heap = new struct cell[MAX_SIZE];
                                              ^
Heap.cpp:11:2: note: candidate: cell::cell(int,int,int)
  cell(int x,int depth):
  ^~~~
Heap.cpp:11:2: note:   candidate expects 3 arguments,0 provided
Heap.cpp:6:8: note: candidate: constexpr cell::cell(const cell&)
 struct cell{
        ^~~~
Heap.cpp:6:8: note:   candidate expects 1 argument,0 provided
Heap.cpp:6:8: note: candidate: constexpr cell::cell(cell&&)
Heap.cpp:6:8: note:   candidate expects 1 argument,0 provided
Heap.cpp: In member function 'void minHeap::insert(cell)':
Heap.cpp:59:24: error: missing template arguments before '(' token
   while(i!=0 && greater(heap[parent(i)],heap[i])){
                        ^
Heap.cpp: In member function 'void minHeap::heapify(int)':
Heap.cpp:73:30: error: missing template arguments before '(' token
   if((left < size) && greater(heap[left],heap[smallest])){
                              ^
Heap.cpp:76:32: error: missing template arguments before '(' token
   if((right < size) && (greater(heap[right],heap[smallest]))){

我对 C++ 比较陌生,所以我不确定我是否提供了所有必要的信息。如果我能提供任何其他详细信息,请告诉我。

解决方法

因为单元格有一个带参数的构造函数,但没有列出默认(无参数)构造函数,所以你会得到第一个错误。

您要么需要创建一个默认构造函数,要么使您拥有的构造函数的参数采用默认值。

解决这个问题,看看有多少其他错误消失了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。