繁体   English   中英

关系运算符重载在 C++ 中不起作用

[英]relational operator overloading not working in C++

我正在尝试使用定制的priority_queue在C++中实现霍夫曼编码。 我将信息存储在名为 Node.js 的结构中。 这是代码

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//two same name functions are for min heap and max heap(Function overloading)
struct Node{
    char letter;
    int value;
    Node* left;
    Node* right;
    Node(char letter,int value, Node* left,Node* right){
        this->letter = letter;
        this->value = value;
        this->left = left;
        this->right = right;
    }

    bool operator < (const Node* &a){//HERE IS THE PROBLEM
        if((this->value) < (a->value))
            return true;
        else
            return false;
    }

    bool operator > (const Node* &a){//HERE IS THE PROBLEM
        if((this->value) > (a->value))
            return true;
        else
            return false;
    }
};

template <class T>
class Priority_Queue{

    public:
    int k;
    int sz;
    vector<T> v;

    Priority_Queue(int k){
        sz = 0;
        this->k = k;
    }

    void heapify(int index,int n){
        int max_index = index;
        for(int i = index*k+1;i <= min(n-1,index*k+k);++i){
            if(v[i] > v[max_index])
                max_index = i;
        }
        if(index != max_index){
            swap(v[max_index],v[index]);
            heapify(v,max_index,n);
        }
    }

    void heapify(vector<int> &v,int index,int n,bool trigger){
        //for calling min_heapify using function overloading
        int min_index = index;
        for(int i = index*k+1;i <= min(n-1,index*k+k);++i){
            if(v[i] < v[min_index])
                min_index = i;
        }
        if(index != min_index){
            swap(v[min_index],v[index]);
            heapify(v,min_index,n,trigger);
        }
    }   

    void Insert(T val){
        if(sz == (int)v.size()){
            v.push_back(val);
            ++sz;
        }
        else
            v[sz++] = val;
        int parent = (sz-1)/k;
        int node = sz-1;
        while(node >= 1){
            int parent = (node-1)/k;
            if(v[parent] < v[node]){
                swap(v[parent],v[node]);
                node = parent;
                parent = (parent-1)/k;
            }
            else
                break;
        }
    }

    void Insert(T val, bool trigger){
        if(sz == (int)v.size()){
            v.push_back(val);
            ++sz;
        }
        else
            v[sz++] = val;
        int parent = (sz-1)/k;
        int node = sz-1;
        while(node >= 1){
            int parent = (node-1)/k;
            if(v[parent] > v[node]){// IF CONDITION DOESN'T WORK
                swap(v[parent],v[node]);
                node = parent;
                parent = (parent-1)/k;
            }
            else
                break;
        }
    }

    void Pop(){
    if(sz == 0){
            cout << "Heap Underflow\n";
            return;
        }
        swap(v[0],v[sz-1]);
        --sz;
        heapify(0,sz);

    }

    void Pop(bool trigger){
        if(sz == 0){
            cout << "Heap Underflow\n";
            return;
        }
        swap(v[0],v[sz-1]);
        --sz;
        heapify(0,sz,trigger);
    }

    T Top(){
        return v[0];
    }

    void printHeap(){
        for(int i = 0; i < sz;++i){
            cout << v[i]->value << " ";
        }
        cout << "\n";
    }

};

int main()
{
    string s;
    cin >> s;
    int n = s.length();
    vector<int> freq(26,0);
    for(int i = 0; i < n;++i){
        ++freq[s[i]-'a'];
    }
    Priority_Queue<Node*> pq(2);
    for(int i = 0; i < 26;++i){
        if(freq[i] == 0)
            continue;
        pq.Insert(new Node(char(i+'a'),freq[i],NULL,NULL),true);
    }
    pq.printHeap();
}

我对 C++ 没有太多经验,并且在重载关系运算符时遇到了麻烦,我想根据 value 参数比较两个结构指针。例如:如果我输入aab ,则应该执行 Insert function 中的 if 条件,但它永远不会发生。 我搜索了很多关于关系运算符重载的信息,但它们似乎都没有解决我面临的问题。 有人可以帮帮我吗? 重载运算符时我做错了什么?

您可能应该使您的方法如下所示:

bool operator<(const Node &node) const {
   return value < node.value;
}

请注意略有不同的方法签名。 然后你应该用一个绝对最小的方法来测试它。

int main(int, char **) {
     Node first{'a', 10, nullptr, nullptr};
     Node second{'b', 5, nullptr, nullptr};

     cout << "first < second: " << (first < second) << endl;
     cout << "second < first: " << (second < first) << endl;
}

在您的代码中,您可能必须这样做:

if (*v[index] < *v[otherIndex)

另一条评论:不要将变量命名为v 随着您的程序变得越来越大,这将在背后咬您一口。 给他们更长的名字,更易于搜索和描述。 vecv好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM