如何将元素从结构插入最小优先级队列

发布于 2025-02-06 20:23:22 字数 2188 浏览 1 评论 0原文

我正在使用C ++中的Huffman算法制作文件压缩机。我已经计算了字符频率,但是现在我很难将其推入最小的优先级队列。我想按频率对插入的元素进行排序。每次执行代码时,都会出现错误:

Error C2676 binary '>': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator

我已经尝试了此网站上提到的几乎所有方法,但是我仍然无法摆脱此错误。

这是我的代码:

#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;

struct node
{
    char c; //character in the string
    int f; //Frequency of character in the string
    node* next;
    node* left, * right; //left and right child of binary tree respectively

    node()
    {
        f = 0;
        left = NULL;
        right = NULL;
        c = NULL;
        next = NULL;    
    }

    bool operator>(const node& a)
    {
        return a.f > f;
    }

    };

class Huffman
{
    string text; //The text that will be encoded
    priority_queue <node> pq;
public:
    Huffman()
    {
        
    }

    void StringInput()
    {
        cout << "Enter the string you want to encode:";
        getline(cin, text);
    }

    //Function which will calculate the frequency of characters in the string entered by the user
    void CharacterFrequency()
{
        
        for (int i = 0; i < text.length(); i++)
        {
            int sum = 0;
            for (int j = 0; j < text.length(); j++)
            {

                if (j < i and text[i] == text[j])
                {
                    break;
                }


                    if (text[i] == text[j])
                    {
                        sum++;
                    } 
                    
                    
                
            }

            if (sum != 0)
            {
                PriorityQueue(text[i], sum);
            }
        }
}

void PriorityQueue(char ch, int freq)
    {
        
        node n;
        n.c = ch;
        n.f = freq;
        pq.push(n);
        

        
    }


};

int main()
{
    Huffman obj;
    obj.StringInput();
    obj.CharacterFrequency();
    
    return 0;
}

在这方面,我将感谢任何帮助。

I am making a file compressor using Huffman Algorithm in C++. I have calculated the character frequency, but now I am having difficulty in pushing it into a min heap priority queue. I want to sort the inserted elements by frequency. Every time I execute the code, it gives the error:

Error C2676 binary '>': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator

I have tried almost every way mentioned on this website, yet I still cannot get rid of this error.

Here is my code:

#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;

struct node
{
    char c; //character in the string
    int f; //Frequency of character in the string
    node* next;
    node* left, * right; //left and right child of binary tree respectively

    node()
    {
        f = 0;
        left = NULL;
        right = NULL;
        c = NULL;
        next = NULL;    
    }

    bool operator>(const node& a)
    {
        return a.f > f;
    }

    };

class Huffman
{
    string text; //The text that will be encoded
    priority_queue <node> pq;
public:
    Huffman()
    {
        
    }

    void StringInput()
    {
        cout << "Enter the string you want to encode:";
        getline(cin, text);
    }

    //Function which will calculate the frequency of characters in the string entered by the user
    void CharacterFrequency()
{
        
        for (int i = 0; i < text.length(); i++)
        {
            int sum = 0;
            for (int j = 0; j < text.length(); j++)
            {

                if (j < i and text[i] == text[j])
                {
                    break;
                }


                    if (text[i] == text[j])
                    {
                        sum++;
                    } 
                    
                    
                
            }

            if (sum != 0)
            {
                PriorityQueue(text[i], sum);
            }
        }
}

void PriorityQueue(char ch, int freq)
    {
        
        node n;
        n.c = ch;
        n.f = freq;
        pq.push(n);
        

        
    }


};

int main()
{
    Huffman obj;
    obj.StringInput();
    obj.CharacterFrequency();
    
    return 0;
}

I will be grateful for any help in this regard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心空情空意 2025-02-13 20:23:22

有两个问题:

问题1 :您没有正确声明优先级队列。

如果要添加自定义比较,则需要在模板中指定该比较:

std::priority_queue <node, std::vector<node>, std::greater<node>> pq;

现在将使用node :: operator&gt;


问题2 操作员&gt;应该是const函数:

bool operator>(const node& a) const
{
    return a.f > f;
}

There are two issues:

Issue 1: You are not declaring the priority queue correctly.

If you want to add a custom comparison, you need to specify that in the template:

std::priority_queue <node, std::vector<node>, std::greater<node>> pq;

Now the node::operator > will be used.


Issue 2: The operator > should be a const function:

bool operator>(const node& a) const
{
    return a.f > f;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文